Search code examples
react-gtm-module

How to remove datalayer from history event with react-gtm-module


When I add a custom datalayer as described here it will fire a message that can be seen in the G-Tagmanager with the added datalayer, but it will also show in every rout-/ history change event after that.

Here is what I have so far:

const tryToSendGADataLayer = () => {
    const tagManagerArgs = {
      dataLayer: {
        ecommerce: {
          purchase: {
            actionField: {..},
            products: [{..}]
          }
        }
      },
      dataLayerName: 'ecommerce'
    }
    TagManager.dataLayer(tagManagerArgs)

}

I only want the datalayer to be added in the Message event and not in all the events after that.


Solution

  • window.google_tag_manager[ GOOGLE_TAG_MANAGER_ID].dataLayer.reset()
    

    GTM does not reset dataLayer on page navigation