This Google Tag Manager Server-Side variable allows you to retrieve a user profile from Klaviyo using the identifier (_kx). It enriches your server-side data with user information such as email, phone number, and address.
How it works
- Known Email Shortcut (optional): If Output is set to Email, Skip API call for known email is checked, and the provided Email Address is a valid email, the variable returns that email (trimmed and lowercased) right away. It skips the identifier lookup, the cache and the API call. Otherwise, it continues with the steps below.
- Identifier Detection: The variable first looks for the
_kxquery parameter in the page URL. - Fallback: If the URL parameter is not present, it attempts to retrieve the identifier from the
stape_klaviyo_kxcookie. - Second Fallback: If neither is present, it reads the
__kla_idcookie set by Klaviyo’s onsite JS, decodes it, and uses its$exchange_idas the identifier. - Cache Check: The variable checks the
templateDataStorageto see if the user data for this identifier is already cached, preventing unnecessary API calls. - API Request: If no cache is found, it sends a GET request to the Klaviyo Profiles API.
- Output: It returns either the user’s email or a full user data object based on your configuration.
Parameters
- Private API Key: Input your Klaviyo Private API Key. This is required to authorize the request. Learn more.
- Output: Select the type of data to return:
- Email: Returns only the user’s email address string.
- All User Data: Returns a JSON object containing the user profile.
- Skip API call for known email (only available when Output is Email): Check this box to provide an email you already know, such as an Event Data variable mapped to the user’s email, and avoid the Klaviyo API call.
- Email Address: The email to return. It is trimmed and lowercased before it is returned, to match the format of emails fetched from Klaviyo. If the value isn’t a string or doesn’t look like a valid email address, the variable ignores it and falls back to the regular Klaviyo lookup.
Returned Data Structure
If All User Data is selected, the variable returns an object containing the following attributes:
{
"email": "user@example.com",
"phone_number": "+1234567890",
"address": [
{
"first_name": "john",
"last_name": "doe",
"street": "123 main st",
"city": "new york",
"postal_code": "10001",
"country": "us"
}
]
}
Open Source
Initial development was done by Lars Friis.
Klaviyo Lookup Variable for GTM Server Side is developed and maintained by Stape Team under the Apache 2.0 license.