Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement GTM with reactjs

I am trying to implement GTM with reactjs. I have used react-google-tag-manager but it did not solve the purpose. Somehow, the data layer needs to be in a particular format and also the needs to be right below the tag, but it is only one of them that i can achieve at a time. I tried placing the code directly in template.html and call the function from the component i wanted, but that didn't work.

import React from 'react';
import gtmParts from 'react-google-tag-manager';

class GoogleTagManager extends React.Component {
   componentDidMount() {        
       const dataLayerName = this.props.dataLayerName || 'dataLayer';
       const scriptId = this.props.scriptId || 'react-google-tag-manager-gtm';

       if (!window[dataLayerName]) {
           const gtmScriptNode = document.getElementById(scriptId);

           eval(gtmScriptNode.textContent);
       }
   }

   render() {

       const gtm = gtmParts({
           id: this.props.gtmId,
           sourcegroup: this.props.gtmGroupname,
           sourceid:this.props.gtmSource,
           age:this.props.age,
           mtongue:this.props.gtmMtongue,
           city:this.props.city,

       });

       return (
           <div>
               <div>{gtm.noScriptAsReact()}</div>
               <div id={this.props.scriptId || 'react-google-tag-manager-gtm'}>
                   {gtm.scriptAsReact()}
               </div>
           </div>
       );        
   }
}

export default GoogleTagManager;

I am pushing parameters in DataLayer and on checking on google tag assistant addon, whole the datalyer is empty.

like image 212
learner Avatar asked Apr 09 '18 13:04

learner


People also ask

How to integrate Google Tag Manager with react router links?

The answer lies in the Google tag manager console. The tag manager's History Change trigger can track all the client-side navigations using React router links. But you still need to insert the tag manager scripts somewhere in your application. Another option is to use the react-gtm-module package from npm.

What is the use of GMT start event in react?

Additional events such as 'gtm.start': new Date ().getTime (),event:'gtm.js'. used to set environments. used to set environments, something like env-00. Disabling javascript in the browser can prevent the correct operation of this library if React is only being rendered on the client side.

How to use React-GTM-module?

How to use react-gtm-module? edit your package.json file and add "react-gtm-module": "^2.0.11" as a dependency. Now we have to place the tracking code in the application.

How to track client-side Navigations using React router links?

The tag manager's History Change trigger can track all the client-side navigations using React router links. But you still need to insert the tag manager scripts somewhere in your application. Another option is to use the react-gtm-module package from npm.


1 Answers

I had this issue yesterday and to solve it, I had to put all the properties that I'm trying to record under the additionalEvents property. Something like this:

const gtm = gtmParts({
    id: this.props.gtmId,
    additionalEvents: {
         sourcegroup: this.props.gtmGroupname,
         sourceid:this.props.gtmSource,
         age:this.props.age,
         mtongue:this.props.gtmMtongue,
         city:this.props.city
    }
})

And also avoid using eval() since this is a dangerous pratique. Update your code like this:

if (!window[dataLayerName]) {
    const script = document.createElement("script")
    const gtmScriptNode = document.getElementById(scriptId)
    const scriptText = document.createTextNode(gtmScriptNode.textContent)

    script.appendChild(scriptText)
    document.head.appendChild(script)
}
like image 167
lomse Avatar answered Sep 21 '22 17:09

lomse