Google Analytics Tracking Code and Actions
In this post I will break down the elements of your Google Analytics Tracking code and cookies.
Your tracking code can be found in your analytics admin under property settings, It will look similar to this:
<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-*********-***”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘UA-*******-**’);
</script>
- Ga uses ga() command queue for various tracking methods including:
- Create: creates a new tracker instance with the specified fields
- Send: sends a hit to google analytics
- Require: dependant on analytics.js plugin
- Provide: Provides analytics.js plugin and its methods for use with the ga() command queue
Command syntax
Ga(command, […fields],[fieldsObject])
Example: ga(‘create’,’UA-*******-*’,’auto’);
- GA requires a tracker object for data collections
- Tracker objects collect and store data and then send that data to Google Analytics
- Default implementation creates tracker object by running the following command:
ga(‘create’,’UA-******-*’,’auto’);
- Default implementation creates tracker object by running the following command:
- This command creats a tracker object by passing “UA-*****-*
as the Tracking ID and “aucto” for the configured domain name - Without setting a specific name for the tracker the defualt tracker is internally given the name “to”
- To change the tracker object’s name, use the folling command:
ga(‘create’, ‘UA-*******-*’, ‘ auto’, ‘ TrackerName’);
- To change the tracker object’s name, use the folling command:
Configure Cookie and Session
A browser cookie is being used to determine that two or more distinct hits belong to a user
GATC generates a unique randomly generated Client ID and stores it in a first party cookie. By default the cookie name is _ga. This cookie is allocated to the top level domain and all of its subdomains, other domains require cross-domain tracking.
GATC generated the cookie by this command: ga(‘create’,’UA-*******-*’,’Auto‘);
Tracker object
If you customize the tracker object name all other commands should be sent to that object
With default object:
ga(‘send’,’pageview’);
With custom name:
ga(‘TrackerName.send’,’pageview’);
Configure Cookie and Sessions
To customize the cookie use the following command:
ga(‘create’,’UA-*******-*’, {
‘cookieName’:’yourCookieName’,
‘cookieDomain’:’store.devdirt.net’, <default is document.location.hostname>
‘cookieExpires’:60*60*24*28 <sets cookie expirey time in seconds,default is 63072000 or two years>
});
Expiry time doesn’t have anything to do with session timeout. Session timeout can be set in GA admin panel under Tracking info>Session settings.
Full list of tracking objects can be found here.