Introduction
I had lots of issues getting setup with Universal Analytics (UA) with a one page app I've had to work on very recently. This tutorial will not explain very much because I believe the issue with UA is not that it's hard, but rather that there's a lack of information out there. This will help you get started on tracking pageviews for your one page application.
Prerequisites:
- A Google Tag Manager Universal Analytics (UA) account
- A Google Analytics account or other analytics platform (but this tutorial won't cover those)
1. Create a Data Layer
A data layer stores information from our code and passes that on to UA automatically.
All you need to do is declare a dataLayer array. Optionally it can have default values for your variables.
<!-- Google Tag Manager Data -->
<script type="text/javascript">
var dataLayer = [{'variableName' : 'variableOptionalValue'}];
</script>
<!-- Google Tag Manager Code -->
...
<!-- End Google Tag Manager Code -->
Next, add an event listener (such as onClick) to your HTML so you know when the user interacts with something.
<a onClick="uaClick">
And finally, add the data to the dataLayer so UA can keep track of it.
$scope.uaClick = function(pageName) {
dataLayer.push({'pageName': 'index'});
};
Notes:
- Make sure the dataLayer instantiation comes before the Google Tag Manager Code
- Use dataLayer.push({'variableName' : 'variableData'});
2. Creating a Variable
- Go to the Variables tab on the left menu.
- Create a new User-Defined Variable.
- Set Variable Name to anything, set Data Layer Variable Name to your variableName.
- Data Layer Version to Version 2 and optional default value.
3. Creating a Trigger
- Go to the Triggers tab.
- Create a new Trigger.
- The event we are probably listening for is a Click event, but this is where things may become ambiguous.
- I wanted all the click events from the variableName to be registered so I did:
variableName | matches RegEx | \w* |
This matches any word or letter and number combination.
5. The rest of the options should be self explanatory.
4. Creating a Tag
Now we are bringing together the components we have created from the other parts. Note that for testing on localhost, you need to set
- Go to the Tags tab.
- Create a new Tag.
- Set tracking ID to your Google Analytics tracking ID.
- Set Track Type to Page View.
- Under Basic Configuration, set Document Path to {{Page URL}}/{{Variable Name}} to create the page URL.
- Set Document Title to {{Variable Name}} to set the page
- If you are testing on localhost, go to Advanced Configuration and set Use Debug Version to True.
If you have any questions, send me a message [here](http://rileedesign.com).
Resources:
- http://cutroni.com/blog/2012/10/01/all-about-google-tag-manager/
- https://developers.google.com/analytics/devguides/collection/upgrade/reference/gtm
- https://support.google.com/tagmanager/answer/6106716
- http://moz.com/ugc/tracking-google-analytics-events-with-google-tag-manager
- https://developers.google.com/tag-manager/devguide
- http://www.razorsocial.com/google-tag-manager/
Support Forums:
- https://productforums.google.com/forum/?utm_medium=email&utm_source=footer#!forum/tag-manager
- http://stackoverflow.com/questions/tagged/universal-analytics
Comments