This time I will start with macros first. As mentioned when we do event tracking, we need to pass several variables (e.g. category, action, etc) to the _trackEvent JavaScript function. in GTM we will first define them accordingly as macros: Choose the Macro Type as “Data Layer Variable” and save the macro. You should see there’re 5 more macros when you’re done:
Tags The next step is to create the actual tag that the page will load, in our case, the Google Analytics event tag. When you create a new Google Analytics tag, it allows you to specify whether it’s an event tag or not:
Once you’ve selected Event Tracking, you will see the Event Tracking Parameters section. Remember you’ve defined them all as macros already? Simply select them from the pulldown and it should look like this when you’re done: Rules
The last configuration we need is the rule to trigger the tag we created. Let’s assume whenever there’s an event “GAEvent” appears in a page, we will load the tag. Click the “Add Rule to Fire tag” button below (you should still stay in the create new tag page) and create a new rule as follows:
Save the rule and the tag and we’re done with all the required setup in GTM already! But wait…how can all these linked up together and what JavaScript function should I be using when events appear? All the magic happens with the GTM DataLayer. According to developers guide from Google: the data layer is an object that contains all of the information that you want to pass to Google Tag Manager So let’s say in your ecommerce site you want to tell GTM the total revenue generated on the confirmation page, you will need to define a data layer object and associate the key and corresponding values in pairs as follows: 1 2 3 dataLayer = [{ 'ecomValue': '2000' }]; Just simple as that. By default the data layer object is using the variable name ‘dataLayer’, but you can change it if you want. You can find more information from here. So in order to trigger the event tag and pass the required parameters to Google Analytics, we just need to call the push function of the dataLayer object as follows: 1 2 3 4 5 6 dataLayer.push({ 'eventCategory': 'the event category you want', 'eventAction': 'the event action you want', 'eventLabel': 'the event label you want', 'event': 'GAEvent' }); Suddenly everything comes together right? To break it down: You put ‘GAEvent‘ as the ‘event’ value, so that the rule you’ve defined will be matched and thus firing the tag you’ve specified You put the values that you want to pass into the event tracking tag by matching the macros you’ve defined (so it’s IMPORTANT to declare those macros as data layer variables . GTM will carry forward these values and put them to the actual event tracking tag Now go to your Google Analytics and the event data should be ready for your visit. The data layer approach introduced in GTM can be very useful and powerful, will try to find more examples on how to make use of this killer feature to make our lives easier. Related posts: Source Web page ..http://www.whymeasurethat.com/2012/10/31/google-tag-manager-part-2-event-tracking/
No comments:
Post a Comment