Back

This article describes how to use a visitor function to generate a UID2.0 token for a user and save it in the visitor profile. This token can be used in The Trade Desk and other supported outbound connectors.

In this article:

What is UID2.0?

Unified ID 2.0 (UID2) is an open source ID framework created by The Trade desk, which can be used instead of of third-party cookies. UID2 is a deterministic user identifier based on user PII, such as an email address. The UID is hashed and encrypted to create a UID2 token that is returned in the response to a UID2 request. For more information, see the Trade Desk UID2 documentation.

Generating a UID2.0 and Storing it in a Visitor Profile

We recommend using a visitor function to generate the UID2 token. For more information on visitor functions, see Event and Visitor Functions.

The visitor function does the following:

  • Generates a UID2 token if the visitor does not have a UID2 token assigned.
  • Sends an event containing the UID2 token and tealium_visitor_id to Tealium Collect.

The AudienceStream attribute and visitor attribute are enriched by the event sent from the function. The following figure illustrates the process of getting an UID2 token and saving it in the visitor profile:

UID2_process_3.png

Prerequisites

  • Choose a PII attribute to use as an identifier for your users. UID2.0 currently supports a phone number or an email address identifier.
  • Create a visitor attribute to store the UID2 token. For more information on creating attributes, see Using Attributes.
  • Create an event attribute for the UID2 token, which will be used to enrich the visitor attribute. For more information on creating attributes, see Using Attributes.
  • Add an enrichment to enrich the UID2 visitor attribute with the value of the event UID2 attribute. For more information on adding an enrichment, see Using Enrichments.

Creating an Audience to Trigger the Function

To trigger your function, create an audience with the following conditions:

  • Identifier is assigned. Identifier is the PII attribute selected to identify your users.
  • Token is not assigned. Token is the attribute in which the UID2.0 token is stored after it is generated.

For example:

uid2-function-trigger-example.png

For more information on creating an audience, see Building an Audience.

Creating a Visitor Function to Generate a UID2 Token

When you create your function and select the audience, leave Trigger On set to the default value, which is Joined Audience.

An API key is required to send requests to the Unified ID 2.0 API. Tealium has partnered with The Trade Desk to provide a managed API key that can be used in functions. After you have created your function, add UID2 authentication to your function to obtain the UID2.0_Tealium API key. 

You can delete the default code that is displayed for a visitor function, then copy and paste the example code below and modify it as needed.

For more information on creating a function, see Managing Functions.

Example Function Code

This example function uses an email address as the user identifier, but can be adapted to use other identifiers.

In this code, an attribute ID is used to get the value of the email attribute. There is a comment above this line of code indicating what needs to be updated. Update the attribute ID with the correct value for the attribute you're using.

import { visitor, auth, tealium } from "tealium";
import CryptoES from 'crypto-es';

const options = {
    "sendHashedOnly": false
};


function validateEmail(email) {
    const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email.trim()).toLowerCase());
}

function isGmail(email) {

    const re = /([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@gmail([\.])com/g;
    return re.test(String(email).toLowerCase());
}

function normalizeEmail(email) {
    /* Start specific normalization for gmail.com addresses */
    if (isGmail(email)) {
        email = email.split("@")[0].replace(/\./g, '') + "@" + email.split("@")[1];
        email = email.replace(/\+/g, "");
    }
    /* End gmail normalization */

    //Trim email
    email = email.trim();

    return email;
}

function checkSHA256(emailHash) {
    // Regular expression to check if string is a SHA256 hash
    const regexExp = /^[a-f0-9]{64}$/gi;

    // String with SHA256 hash
    const str = emailHash;

    return regexExp.test(str); // true
}

async function runFunction() {
    const data = {
        email: ""
    };
    let bodyObj = {};
    data.email = visitor.getAttributeValueById("5006");
    if (validateEmail(data.email)) { //Unencrypted email
        data.email = normalizeEmail(data.email);
        if (options.sendHashedOnly) {
            bodyObj = {
                email_hash: [CryptoES.SHA256(data.email).toString(CryptoES.enc.Hex)]
            };

        } else {
            bodyObj = {
                email: [data.email]
            };
        }
    } else if (checkSHA256(data.email)) {
        // It's a hash
        bodyObj = {
            email_hash: [data.email]
        };
    } else {
        return false; //Non valid email
    }
    const url = 'https://integ.uidapi.com/identity/map';
    if (url) {
        console.log(auth.get('UID2.0_Tealium'))
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Authorization': ' Bearer ' + auth.get('UID2.0_Tealium'),
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(bodyObj)
        });
        return response;
    } else {
        return false; // A url hasn't been provided.
    }
};

runFunction().then(response => {
    let event_data = {
        tealium_event: "UID 2.0 sync",
        identifier: JSON.parse(response.body).mapped[0].identifier,
        UID2: JSON.parse(response.body).mapped[0].advertising_id,
        tealium_visitor_id: visitor.properties.visitor_id
    };
    if (event_data.tealium_visitor_id && event_data.UID2) {
        tealium.sendCollectEvent(event_data);
    }
});

Additional Information

Public