@jaycee_rowe
To use Google Tag Manager in Node.js, you will need to install the gtm
package using npm. Here are the steps to install and use Google Tag Manager in Node.js:
- Install the gtm package:
- Require the gtm package in your Node.js file:
1
|
const gtm = require('gtm');
|
- Initialize Google Tag Manager with your GTM ID:
1
2
|
const gtmId = 'GTM-XXXXXX'; // Replace 'GTM-XXXXXX' with your GTM ID
const container = gtm(gtmId);
|
- Send data to Google Tag Manager:
1
2
3
4
5
|
// Send an event to Google Tag Manager
container.sendEvent({
event: 'customEvent',
customKey: 'customValue'
});
|
- Handle errors:
1
2
3
4
|
// Handle errors when sending data to Google Tag Manager
container.on('error', err => {
console.error('Error:', err);
});
|
- Run your Node.js file to start sending data to Google Tag Manager:
By following these steps, you can easily use Google Tag Manager in your Node.js application to track events and send data to Google Analytics or other tracking tools.