Register user profiles

Sending your users in with a profile will lead to better conversion, higher revenues, and improved user experience (Optional):

  1. If you want to pass your user’s profile, map your user attributes to our targeting attributes and generate a payload.

  2. Encrypt the payload

  3. Invoke the following redirect URL for your user with encrypted payload:

https://dlink.peanutlabs.com/direct_link/?pub_id=xxxx&user_id=xxxx&sub_id=xxxx&iv=xxxx&payload=xxxx

Encrypting the Payload

If you already have user profiled information on your end, you can send it to us using the payload parameter on the query string. Please map as many of your user’s attributes to Peanut Labs attributes for better targeted survey for your users. Invalid user profiles will be rejected.

The basic steps for the encryption are:

  1. Create your JSON object

  2. Serialize it to a string/GetBytes

  3. Get your Security key in the Portal.

  4. Convert the security key into a bytes object (“Pack” the security key).

  5. Generate a cryptographically random AES CBC mode initialization vector (block size 128 bits) a. save a base64 encoded copy of your IV for the IV parameter of the request Body

  6. Add AES padding to your serialized string/bytes (block size 128 bits)

  7. With your packed Security Key as the secret key, and the initialization vector you generated in step 4 (pre-base64 encoding), encrypt the padded string/bytes with AES CBC mode.

  8. base64-encode the ciphertext for the payload parameter of the request body

Payload

The payload is included as a JSON object including the following:

  1. user_id

    Dynata UID - three part User ID

  2. cc

    ISO ALPHA-2 code e.g. US, DE, GB

  3. dob

    (YYYY-MM-DD)

  4. panelist sex

    1 for male, 2 for female

  5. postal

    Zip/postal code

  6. profile_data

    Collection of key-value pairs of other profiling attributes

Key Pair Mapping for Profile Data

Profile Data is a JSON object composed of user specific profile data values and profile attributes pairs, mapped according to the Peanut Labs user map document. It is important the information is accurate and specific to ensure that your users are shown the appropriate surveys. Here is an example of one user’s profile and how it may be properly mapped into a JSON payload.

Construct the mapped attributes into a JSON payload such as the following:

{"user_id":"user001-1001-2389d74882","cc":"GB","sex":1,"dob":"1990-04-10","postal":"CB1 0AX","profile_data":{"q122":["qx122-0"],"q159":["qx159-1"],"q102":["qx102-105"],"q158":["qx158-3"],"q101":["qx101-2"],"q157":["3"],"ch-m":["4-2004"],"ch-f":["10-1998","11-1999"]}}

Code Example Below:

Python
import binascii
from base64 import b64decode
from base64 import b64encode
import urllib

from Crypto.Cipher import AES
from Crypto import Random

def pad_payload(p):
    BLOCK_SIZE = 16  # Bytes
    return p + (BLOCK_SIZE - len(p) % BLOCK_SIZE) *                 chr(BLOCK_SIZE - len(p) % BLOCK_SIZE)

def encode_for_transit(enc_bytes):
    b64 = b64encode(enc_bytes)
    return urllib.parse.quote(b64)

def encrypt(app_key, json_payload):
    padded = pad_payload(json_payload)
    bytes_padded = bytes(padded, encoding='utf-8')

    iv = Random.new().read(AES.block_size)
    pack_key = binascii.unhexlify(app_key)
    cipher = AES.new(pack_key, AES.MODE_CBC, iv)
    encrypted = cipher.encrypt(bytes_padded)

    return iv, encrypted

def format_url(app_id, user, sub_id, iv, payload):
    url = "https://dlink.peanutlabs.com/direct_link/?pub_id={}&user_id={}&sub_id={}&iv={}&payload={}"
    iv = encode_for_transit(iv)
    payload = encode_for_transit(payload)

    return url.format(app_id, user, sub_id, iv, payload)

user = "user001-1001-2389d74882" #Your user id here
json_payload = '{"user_id":"user001-1001-2389d74882","cc":"GB","sex":1,"dob":"1990-04-10","postal":"CB1 0AX","profile_data":{"q122":["qx122-0"],"q159":["qx159-1"],"q102":["qx102-105"],"q158":["qx158-3"],"q101":["qx101-2"],"q157":["3"],"ch-m":["4-2004"],"ch-f":["10-1998","11-1999"]}}'
app_id = 1001 # Your App ID Here
app_key = babcdef0123456789abcdef0123456789' # Your Secret App Key
sub_id = '&iXF0HJLBAM' # Your Sub_id

iv, payload = encrypt(app_key, json_payload)
print(format_url(app_id, user, sub_id , iv, payload))