Example of sending an event to Tealium Collect
This article shows an example function that uses the IP address to get the location city, makes an HTTP GET request to get weather information for that city, and then sends the city and weather information to the Tealium Collect HTTP API.
Event and visitor functions can send data to the Tealium Collect HTTP API, as shown below.
import { auth, visitor, event } from "tealium";
console.log(JSON.stringify(visitor));
const ip_stack_api_key = 'your_api_key',
weather_api_key = 'your_weather_api_key',
ip_address = visitor?.current_visit?.properties?.ip_address;
console.log(ip_address);
(async function() {
if(ip_address) {
try {
// Use IP address to get location city
let city_response = await fetch('https://api.ipstack.com/${ip_address}?access_key=${ip_stack_api_key}&format=1'),
city_data = await city_response.json();
console.log(JSON.stringify(city_data));
// Use location city to get local weather
let weather_response = await fetch(encodeURI('https://api.openweathermap.org/data/2.5/weather?q=${city_data.city}&appid=${weather_api_key}')),
weather_data = await weather_response.json();
console.log(JSON.stringify(weather_data));
// Send city and weather description back into collect endpoint
await fetch(encodeURI('https://collect.tealiumiq.com/event?tealium_account=cloud-functions-usecases&tealium_profile=main&tealium_visitor_id=${visitor._id}&lookup_city=${weather_data?.name}&weather=${weather_data?.weather?.[0]?.description}&country=isp=${city_data?.connection?.isp}'));
} catch(e) {
console.error(e);
return false;
}
} else {
console.error("Could not locate users IP address")
}
})();
This page was last updated: January 7, 2023