Visitor function examples

This article provides examples of visitor functions that send data using HTTP POST or HTTP GET, and a visitor function that .

Sending 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.

// Send visitor data - HTTP POST
import {visitor} from 'tealium';

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));

Sending 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.

// Send visitor data - HTTP GET
import {visitor} from 'tealium';

console.log(JSON.stringify(visitor));

fetch(encodeURI('https://webhook.site/87bb160f-475a-4258-b117-693bb2378a4d?param=${visitor.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));

Was this page helpful?

This page was last updated: January 7, 2023