Skip to content

Python Tagging Example

Tagging users from your backend system is a common way of targeting tips at users in real-time or creating segments.

Its simple to do, you are targeting:

  • the unique ID for a user in your backend system (usually an email address, account number or a UUID). Contextual does not care what the ID is as long as you've sent that ID from the App side (usually when a user logs in). The tag goes to all phones/tablets of the specified user ID (we call this "sh_cuid").
  • The tag name is what you want to call it. In the example below "Plan-49" is how you will see this in the Contextual Dashboard.
  • Contextual supports tags of: string, numeric and datetime. In the example below, the value for "Plan-49" is "Trial-Period".

Setting a String tag.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import requests

# Get the <HOST>, <APP_KEY> and <AUTH_TOKEN> on the Settings Page of the Contextual Dashboard.
HOST = "<...>" 
APP_KEY = "<...>"
AUTH_TOKEN = "<...>"

r = requests.post(
   HOST + '/v3/tags',
    json={
        "sh_cuid": "example@acme.com", # your unique userID, doesn't have to be email address
        "key": "Plan-49",
        "string": "Trial-Period",
        "app_key": APP_KEY,
    },
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Token %s' % AUTH_TOKEN,
    }
)
print(r.json())

Setting a Numeric tag.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
r = requests.post(
   HOST + '/v3/tags',
    json={
        "sh_cuid": "example@acme.com", # your unique userID, doesn't have to be email address
        "key": "Plan-49-TrialDay",
        "numeric": 2,
        "app_key": APP_KEY,
    },
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Token %s' % AUTH_TOKEN,
    }
)

print(r.json())

Setting a Datetime tag.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
r = requests.post(
   HOST + '/v3/tags',
    json={
        "sh_cuid": "example@acme.com", # your unique userID, doesn't have to be email address
        "key": "Plan-49-TrialStart",
        "datetime": "2021-10-25T15:47:44.131817", #  string in ISO 8601 format, YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
        "app_key": APP_KEY,
    },
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Token %s' % AUTH_TOKEN,
    }
)

print(r.json())