When using Google Analytics (GA) directly, you can send custom events to GA in javascript like so:
ga('send', {
'hitType': 'event',
'eventCategory': 'Feedback',
'eventAction': 'event action',
'eventLabel': $(‘#feedback-text’).val(),
'eventValue': 1
});
However, with Google Tag Manager (GTM), you don’t have the ga
javascript object. Instead you can:
- push variables to the GTM dataLayer object
- create a tag in GTM that sends the variables along to GA
Here are the steps:
Replace your call to “ga(‘send’”
Let’s say you use the ga
javascript object to send an event to GA on a form submission:
$('#feedback-form').submit(function(e) {
ga('send', {
'hitType': 'event',
'eventCategory': 'Feedback',
'eventAction': 'event action',
'eventLabel': $(‘#feedback-text’).val(),
'eventValue': 1
});
...
});
With GTM, you’ll do this instead:
$('#feedback-form').submit(function(e) {
dataLayer.push({
'hitType': 'event',
'eventCategory': 'Feedback',
'eventAction': 'General',
'eventLabel': $('#feedback-text').val(),
'eventValue': 1
});
...
});
Next you’ll configure a GTM tag to fire. In this tag you’ll pick up the values from the dataLayer and give them to GA.
In GTM: Map GTM variables to dataLayer variables
Variables > User Defined Variables > New > Data Layer Variable
> Configure Variable > Data Layer Variable Name: eventLabel
(this is the variable name used in the javascript call to dataLayer.push)
I like to name the GTM variable as dataLayer.[variableName], in this case dataLayer.eventLabel.
In GTM: Create a tag to send the dataLayer variable(s) to GA
Tags > New > Google Analytics > Universal Analytics
- Tracking Id: UA-1242343-12 (i.e. your tracking id)
- Track Type: Event
- Event Tracking Parameters
- Create The trigger
- Fire On:
Form > Form Submission > Some Forms > "Form Id” equals “feedback-form"
Test that the variable is sent to Google Analytics
Save the tag and trigger and click the GTM Preview and Debug (each time you make changes to GTM).
In a new tab, visit the site with the feedback form. You should see the GTM debugging pane at the bottom of the page. It should fire our newly created tag when the form is submitted.
Submit the feedback form.
In Google Analytics Reporting > Real-time > Events
You should see an entry for the just-submitted event. Click into it and you will see the eventLabel detail.