QR Code Engagement Workflows Using a Python SDK
Tags: engagement, python, qrcode, tutorial
Published: 2022-06-27 17:38:27

Since the beginning of the pandemic, we have seen QR Codes pop-up virtually everywhere. In fact, there has been a 94% [growth](https://www.bluebite.com/nfc/qr-code-usage-statistics) in QR code usage since 2018!

According to [eMarketer](https://www.emarketer.com/content/brands-leverage-qr-codes), the number of US smartphone users scanning QR codes will increase from 83.4 million in 2022 to a projected 99.5 million by 2025. This is an excellent opportunity for customer engagement agencies to adopt QR Code engagement solutions. 

## Openscreen's QR Engage
[Openscreen](https://www.openscreen.com/) is an end-to-end engagement and insights platform to drive revenue and client loyalty. Openscreen offers a comprehensive python [SDK](https://docs.openscreen.com/overview) which can help you create powerful customer engagement workflows. 

In the example below, we will create a Dynamic QR code which would lead to a web-form, and enter submissions into a draw. A random winner is chosen and an SMS is sent to the winner. 
![Openscreen's QR Engage at the Gatineau Olympiques Minor League Hockey game](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gj2ng71j4ok68esriyyo.jpg)
Here's what you will need for this workflow: 
- A free Openscreen [account](https://openscreen.com/signup)
- [Python 3.10+](https://www.python.org/downloads/release/python-3100/) 
- Openscreen's Python [SDK](https://docs.openscreen.com/overview)
- A custom webform to capture customer's contact information

## Environment Set-up
Before we begin implementing our workflow, we will store the following identifiers in a .env file. They are available on your Openscreen Dashboard:
```
ACCOUNT_ID='xxxxxxxxxxxxxxxxxxxxxx'
OS_ACCESS_KEY='xxxxxxxxxxxxxxxxxxxxxx'
OS_ACCESS_SECRET='xxxxxxxxxxxxxxxxxxxxxx'
PROJECT_ID='xxxxxxxxxxxxxxxxxxxxxx'
```
The following code will authenticate your application:
{% embed https://gist.github.com/birinder-lobana/505d54f729c27f924b754a198074ca71 %}

## Create A Dynamic QR Code
Openscreen's python SDK uses several core objects to ensure the scalability of QR code enabled workflows. 

An Asset is a core object that stores information about a physical or a digital object. The Asset object represents the QR Code. One Asset can have several QR codes associated with it - think of a lottery ticket (the asset) with multiple QR Codes.
 
Creating an Asset and associating a dynamic QR code can be done in a single call. In the code below, I am creating a billboard (Asset) for a real-estate listing. A person scanning this QR code should be led to your web-form requesting information about the customer. After the form is submitted, you can display the information about the listing.
 
{% embed https://gist.github.com/birinder-lobana/c69efc6f69412c7d38eae16fdaa82429 %}

## Capturing QR Code Scans
One unique advantage of using [Dynamic QR Codes](https://www.openscreen.com/openscreen-engage) is that they are traceable. Openscreen's SDK generates a scan object, identified by a scanId each time a dynamic QR is scanned. Additionally, this scanId can be appended as a query string parameter to the destination URL of the QR code. This allows us to create workflows surrounding a single QR code scan!

The following flag sets the scanId in the query string parameter of the QR code destination URL:
```
dynamic_redirect_type: 'SCAN_ID_IN_QUERY_STRING_PARAMETER'
```
## Creating a Contact
Once the QR code is scanned, a scan object is generated, while the user is led to your custom web form. After the user submits the form, your application should fetch the user-details and the scanId from the URL. This will allow you to create a contact against the scanId:

{%embed https://gist.github.com/birinder-lobana/513fe8b1176c7132047ca72a27322105 %}

Note: Openscreen requires contacts to have a consent before you can communicate with the contact. 

Be sure to store this scanId which was used to create this contact. In the above code, I am storing the scanId as one of the custom attributes in the contact object.

This is the extent of your campaign, where you are able to capture consented customer contacts. You can choose to export these contacts to a CRM tool, or to a CSV file. 

## Send SMS
To choose a winner of your campaign, you can find a random contact from your list of contacts. You can fetch all your contacts in this project by calling the [`GetContactsByProjectId`](https://docs.openscreen-dev.com/library/contact#get_contacts_by_account_id) function.  In order to send SMS to this contact, you will need to fetch the scanId. In our case, it is available as one of the custom attributes: 
{% embed https://gist.github.com/birinder-lobana/8cdad947c31eb46cddff400597f9a83c %}

## Conclusion
The above workflow is an excellent example of how QR code enabled solutions can boost customer engagement. In this case, there was a prize incentive, but the model can be reused to showcase new products while providing unique coupon codes, communicate about upcoming events and incentivize ticket purchases, and much more. At the end of the campaigns, you receive a list of consented customer contacts for your future campaigns. 

Read more: [How to Add CRM to Your QR Code Application](https://dev.to/birinderlobana/how-to-add-crm-to-your-qr-code-application-2nmo), [Scan to Cart QR Codes](https://dev.to/birinderlobana/scan-to-cart-qr-codes-2gdc)