Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// An example tag template that makes a request to OpenWeatherMap.org
// and sends data about the weather to both GA4 and Universal Analytics.
const getAllEventData = require('getAllEventData');
const getEventData = require('getEventData');
const JSON = require('JSON');
const sendEventToGoogleAnalytics = require('sendEventToGoogleAnalytics');
const sendHttpRequest = require('sendHttpRequest');
const client_id = getEventData('client_id');
const events = getAllEventData();
sendHttpRequest('https://api.openweathermap.org/data/2.5/weather?lat=52.3746027&lon=6.6810724&appid=[enter_api_key_here]&units=metric&lang=nl', (statusCode, headers, response) => {
response = JSON.parse(response);
// GA4
events.weather = response.weather[0].description; // Add data from the OpenWeatherMap API to the request
sendEventToGoogleAnalytics(events, (response) => {
data.gtmOnFailure();
});
// Universal Analytics
const events_ga = {
'x-ga-measurement_id': 'UA-XXXXXX-1', // Replace with your own GA UA ID
'v': 1,
't': 'event',
'cid': client_id,
'ec': 'example event category',
'ea': 'example event action',
'el': response.weather[0].description, // Add data from the OpenWeatherMap API to the request
'ni': 1
};
sendEventToGoogleAnalytics(events_ga, (response) => {
data.gtmOnFailure();
});
}, {method: 'GET', timeout: 2000});
// Call data.gtmOnSuccess when the tag is finished.
data.gtmOnSuccess();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment