Event and visitor functions examples (V3)
This article provides example code for V3 event and visitor functions.
Send event data with HTTP GET
The following example shows how to make an HTTP GET request to an endpoint with event data as query string parameters.
activate(({ event }) => {
console.log(JSON.stringify(event));
fetch(encodeURI(`https://webhook.site/87bb160f-475a-4258-b117-693bb2378a4d?param=${event.data}`))
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok. Status code: ${response.status}.`);
}
return response.json();
})
.then(data => console.log('Response:', JSON.stringify(data)))
.catch(error => console.log('Error:', error.message));
})
Send visitor data with HTTP POST
The following example shows how to make an HTTP POST request to an endpoint with visitor profile data in the request body JSON.
activate(({ visitor }) => {
console.log(JSON.stringify(visitor));
fetch('https://webhook.site/87bb160f-475a-4258-b117-693bb2378a4d',
{
method: 'POST',
body: JSON.stringify(visitor),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok. Status code: ${response.status}.`);
}
return response.json();
})
.then(data => console.log('Response:', JSON.stringify(data)))
.catch(error => console.log('Error:', error.message));
})
Send visitor data with HTTP GET
The following example shows how to make an HTTP GET request to an endpoint with visitor profile data as query string parameters.
activate(({ visit }) => {
console.log(JSON.stringify(visit)); // notice separate visit property
fetch(encodeURI(`https://webhook.site/87bb160f-475a-4258-b117-693bb2378a4d?param=${visit.creation_ts}`))
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok. Status code: ${response.status}.`);
}
return response.json();
})
.then(data => console.log('Response:', JSON.stringify(data)))
.catch(error => console.log('Error:', error.message));
})
Send an event to the Tealium Collect HTTP API
The following example shows how to fetch data and send an event to Tealium Collect.
activate(async ({ event }) => {
const searchQuery = new URLSearchParams({ path: event.data.dom.pathname, query: event.data.dom.search_query });
const newEvent = await fetch(`https://getnew.event.com?${searchQuery}`)
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok. Status code: ${response.status}.`);
}
return response.json();
});
track(newEvent, {
tealium_account: event.account,
tealium_profile: event.profile,
tealium_datasource: 'p9v81m'
})
.then(response => {
if(!response.ok){
throw new Error(`Network response was not ok. Status code: ${response.status}.`);
}
return response.text();
})
.then(data => console.log('Result : ', data))
.catch(error => console.error('Error:', error.message));
})
Get an authentication token ID
The following code example shows how to get an authentication token ID:
activate(({ helper }) => {
console.log(helper.getAuth("auth_token_name"));
})
Get the value of a global variable
The following code example shows how to get the value of a global variable:
activate(({ helper }) => {
console.log(helper.getGlobalVariable("global_variable_name"));
})
This page was last updated: June 28, 2023