alinemorelli / react-gtm

React Google Tag Manager
MIT License
658 stars 140 forks source link

How to setup properly react-gtm-module #85

Open vinibispo opened 3 years ago

vinibispo commented 3 years ago

Hi, nice work on react-gtm-module, I'm trying to setup this library on my ecommerce, but I couldn't push my datalayer to tag manager, is there a guide or something else?

Environments:

ghost commented 3 years ago

I also have the same question, is there any guides or examples of real world scenario.

JMarchetto commented 3 years ago

Are you trying to push the dataLayer in the initialization itself?

I've been successful pushing the dataLayer by initializing on load, and then calling the dataLayer function as a separate event.

On load:

import TagManager from 'react-gtm-module';

const tagManagerArgs = {
    gtmId: 'GTM-xxxxxxx', // required
};

TagManager.initialize(tagManagerArgs);

Trigger the dataLayer while performing a specific action (we assume TagManager has already been initialized):

import TagManager from 'react-gtm-module';

const tagManagerArgs = {
  dataLayer: {
      event: 'eventName', // event to be picked up by GTM
      customValue1: '', // anything you want here
  }
}

TagManager.dataLayer(tagManagerArgs);
Hamdan85 commented 2 years ago

@JMarchetto but do you know how to add environments to it?