About event and visitor functions

This article provides information on event and visitor functions, and the named exports that are common to both function types.

Event and visitor functions are invoked after the event or visitor is processed. You can use event and visitor functions to retrieve data from other systems, augment Tealium data, or send data to other endpoints. To retrieve data from other systems, the function typically requires authention for that system. For information on adding authentication to an event or visitor function, see Adding an Authentication to a Function.

Input to event and visitor functions

The Tealium module exports five named exports for event and visitor functions, as follows:

Named Export Description
auth Provides a method to get an authentication token from a service provider such as Facebook or Google.
event Contains the event data from the event feed.
visitor Contains the visitor data from the audience feed.
store Provides a method to get a global parameter.
tealium Provides a method to send an event to Tealium Collect.

A function imports the named exports as follows:

import { auth, visitor, event, store, tealium } from "tealium";

The auth, store, and tealium objects are available to event and visitor functions. The event object is available when a function is triggered by an event feed and contains the event data. The visitor object is available when a function is triggered by a visitor feed and contains visitor data. For more information, see Event Object and Visitor Object.

Auth object: auth.get()

Functions require authentication to access some service providers, such as Facebook or Google. For more information, see Add an Authentication to a Function. When you add an authentication, an access token is returned.

The auth.get() method returns a string that contains a unique auto-generated ID for the authentication token. When a function makes an HTTP request using this token ID, it is automatically replaced with the token before the request is sent.

Parameter Data Type Description
tokenKey string Specifies the token to get, which is the name entered when the authentication provider was added.

Tealium object: sendCollectEvent()

The sendCollectEvent() method sends an event to Tealium Collect HTTP API and returns a string.

The Response interface of the Tealium Collect HTTP API represents the response to a request. The response model is the same as for fetch API, except that the URL is not available in the result of the Tealium Collect HTTP API.

Parameter Data Type Description
event EventsClientEventObject An event object to send to Tealium Collect HTTP API.
account string Optional. If specified, replaces the value of event.tealium_account.
profile string Optional. If specified, replaces the value of event.tealium_profile.
dataSourceId string Optional. If specified, replaces the value of event.tealium_datasource.

Store object: store.get()

The store object contains key/value pairs, referred to as global variables, that specify variables or constants that can be used in multiple functions. Global variables can be added, edited, and deleted from the Code tab of the functions code editor. Functions can retrieve the value of a global variable, but cannot modify it.

To retrieve a value for a key, functions call store.get() and pass the key as the parameter. For more information, see Manage global variables.

Parameter Data Type Description
globalParameterKey string     Specifies the key of the global variable to get.

For example, if you create a global variable with a key named date, a function would retrieve the value for date as follows:

store.get('date')

Sending an event to Tealium Collect

The following example shows how to send an event to the Tealium Collect HTTP API. The response interface of the Tealium Collect HTTP API represents the response to a request. The response model is the same as for the fetch API, except that the URL is not accessible in the result from the collect client.

Example

import {event, tealium} from 'tealium';

var newEvent = {key: value};

tealium.sendCollectEvent(newEvent, 'newAccount', 'newProfile', 'datasourceId')
    .then(response => {
        if (!response.ok) {
            throw new Error(`Network response was not ok. Status code: ${response.status}.`);
        }

        console.log('Status code:', response.status);
        return response.text();
    })
    .then(data => console.log('Result:', data))
    .catch(error => console.error('Error:', error.message));


function buildURLSearchParams(params, data, parentKey) {
    if (data && typeof data === 'object' && !(data instanceof Date)) {
        Object.keys(data).forEach(key => {
            buildURLSearchParams(params, data[key], parentKey ? `${parentKey}[${key}]` : key);
        });
    } else {
        const value = data == null ? '' : data;

        params.append(parentKey, value);
    }
}

Was this page helpful?

This page was last updated: January 12, 2023