Search code examples
google-analyticsgoogle-tag-managercodemirror

Google tag manager custom event tracking


I am a developer in codiva.io a java ide for students. I am using codemirror editor. I want to track on edits (specifically a pause or timeout after last edit). I had previously used Google analytics, and for events, we will do ga.send(). Now I'm using Google tag manager, for clicks and other events it seems we can configure using tag manager ui itself. With tag manager, I'm not able to find how implement tracking for this.

Codemirror generates an on change event. On each edit, clear any previous timer and setup a timeout trigger to run after 200ms. (The compilation will be dive at this point, and I want to make sure to track the number of times this event happened)


Solution

  • If I understand you correctly:

    Use you change event to have a custom event pushed to the dataLayer:

    dataLayer.push({event:'compile'});
    

    Then create a trigger of the type "custom event", set event name to "compile" and use that to fire a Google Analytics event tracking tag (if you use the GA tag template you can select the hit type via a dropdown).

    The "push" method of the dataLayer ist not the native array method but a special implementation by the GTM code; GTM uses this to monitor changes to the dataLayer, and the "event" keyword tells GTM to update its internal datastructure so new values become available to tags and triggers.

    With the same push you can also pass data to be used as eventCategory etc:

     dataLayer.push({
        event:'compile',
        eventCategory:'myCategory',
        eventAction:'myAction',
        ....
     });
    

    You then create new variables of the "dataLayer" type and enter the name of the key you want to access. You can then use the variable in your Ga tag, either by selecting it from the autosuggest list (if you click the icon right to the eventCategory etc. fields) or by typing out the variable name with curly brackets, i.e {{myVariableName}}.

    Also keep in mind that the "event" keyword in the dataLayer does not relate to Google Analytics events, except in the sense that it can be used to trigger event tracking (as well as any other tag). "Event" is just a keyword in GTM to indicate that data is updated and tags can now access the new data.