Event function examples
This article provides examples of event functions that send data using HTTP POST or HTTP GET.
Sending event data with HTTP POST
The following example shows how to make an HTTP POST request to an endpoint with event data in the request body JSON.
// Send event data - HTTP POST
import {event} from 'tealium';
console.log(JSON.stringify(event));
fetch('https://webhook.site/87bb160f-475a-4258-b117-693bb2378a4d',
{
method: 'POST',
body: JSON.stringify(event),
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 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.
// Send event data - HTTP GET
import {event} from 'tealium';
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));
This page was last updated: January 7, 2023