Introduction to Google Analytics Commands
In order to set up or troubleshoot any Google Analytics implementation, whether it is ecommerce tracking, cross-domain tracking, event tracking or enhanced ecommerce tracking, you need to understand how the ga() command queue function works.
Following is the syntax of the ga() command queue function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
trackerName is the name of the tracking object. (Optional parameter)
pluginName is the name of the analytics.js plugin. (Optional parameter)
methodName can be a command method or plugin method. (Required parameter)
fields are used based on the command method or plugin method being used. (Optional parameter)
fields-objects are JavaScript objects (Optional parameter)
These parameters are executed in the order in which they are supplied (from left to right) as soon as the analytics.js library is fully loaded.
Difference between arguments and parameters
‘argument’ is the actual value passed to a function when it is called. Whereas ‘parameter’ is a variable used in function declaration/definition to represent an argument.
For example:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
Here, trackerName, pluginName, methodName, fields and fields-objects are parameters of the function ga().
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
Here, ‘testProperty’, ‘linker’, ‘autolink’, [‘xyz.com’], ‘false’ and ‘true’ are arguments (actual values) of the function ga().
It is important that you remember the difference between arguments and parameters.
trackerName
trackerName is the name of a tracking object. It is one of the parameters of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
Tracking object (also known as tracker) is used to send hit data (pageviews, screenviews, events, etc) to a Google Analytics property.
There are two types of trackers:
#1 Default tracker
#2 User-defined tracker
The name of the default tracker is ‘t0’. But we generally do not refer to the default tracker by this name. We generally do not specify a tracker name when referring to the default tracker.
For example:
ga(‘send’, ‘pageview’); // send pageview data via default tracker.
However, if you are using multiple trackers or you do not want to use the default tracker, then you need to specify the name of the non-default tracker. For example:
ga(‘testProperty.send’, ‘pageview’); // send pageview data via the tracker named ‘testProperty’.
Since we do not always use multiple trackers, specifying tracker name is optional in a ‘ga’ function.
pluginName
pluginName is the name of an analytics.js plugin. It is one of the parameters of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘ec:addPromo’,{……………});
here ‘ec’ is the enhanced ecommerce plugin which is used to set up enhanced ecommerce tracking.
Another example:
ga(‘linker:autoLink’, [‘xyz.com’], false, true);
here ‘linker’ is a linker plugin which is used to set up cross-domain tracking.
If you are using multiple trackers or you do not want to use the default tracker, then you need to specify the name of the non-default tracker while using a plugin. For example:
ga(‘testProperty.ec:addPromo’,{……………});
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
here I am using the ‘ec’ and ‘linker’ plugins with a non-default tracker called ‘testProperty’.
Since we don’t always use plugins, specifying a plugin name is optional in a ‘ga’ function.
methodName
methodName can be a command method or plugin method.
Following are the examples of command methods:
#1 create – used to create a tracking object. Example: ga(‘create’, ‘UA-XXXX-Y’, ‘auto’);
#2 send – used to send hit data to Google Analytics. Example: ga(‘send’, ‘pageview’);
#3 set – used to set field names and values. Example: ga(set, ‘page’, ‘/home);
#4 require – used to set up analytics.js plugins. Example: ga(‘require’, ‘ec’);
#5 provide – used to provide analytics.js plugin and its methods for use with the ga() function.
#6 remove – used to remove a tracking object. Example: ga(‘myTracker.remove’);
Following are the examples of plugin methods:
#1 addImpression – used to measure product list view data in enhanced ecommerce tracking. Example: ga(‘ec:addImpression’,{…});
#2 addProduct – used to measure: product list clicks, product detail views, Product Add to Cart, Product Remove from Cart, Checkout Steps, Purchase and Refund data. Example: ga(‘ec:addProduct’,{…});
#3 SetAction – used to measure action data. Example: ga(‘ec:setAction’, ‘click’, {…});
#4 autoLink – used to define domains that need to be automatically cross-linked. Example: ga(‘linker:autoLink’, [‘destination1.com’, ‘destination2.com’]);
methodName is the only ‘required’ parameter of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example: ga(‘send’, ‘pageview’);
Here, ‘send’ is a command method used to send pageview hit.
Another example: ga(‘create’, ‘UA-12345-1’, ‘auto’);
Here, ‘create’ is a command method used to create a default tracker.
If you are using multiple trackers or you do not want to use default tracker, then you need to specify the name of the non-default tracker while using command or plugin methods.For example:
ga(‘testProperty.send’, ‘pageview’);
Here, ‘testProperty’ is a tracker name, ‘send’ is the command method used for ‘testProperty’
Another example,
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
Here, ‘testProperty’ is a tracker name, ‘linker’ is a plugin name and ‘autolink’ is the plugin method.
Note: you can’t use ‘testProperty.create’ as in:
ga(‘testProperty. create’, ‘UA-12345-1’, ‘auto’);
for creating a non-default tracker. It won’t work. The correct way to create a non-default tracker is mentioned below:
ga(‘create’, ‘UA-12345-1’, ‘auto’, {‘name’:’testProperty’});
Here, ‘create’ is a method used to create a new non-default tracker called ‘testProperty’
fields
fields are optional parameters of the ga() function which are used based on the command method or plugin method being used.
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘create’, ‘UA-12345-1’, ‘auto’);
Here, ‘UA-12345-1’ and ‘auto’ are the two fields of the ‘create’ command method.
Another example,
ga(‘linker:autoLink’, [‘xyz.com’], false, true);
Here, [‘xyz.com’], ‘false’ and ‘true are the various fields of the linker plugin method.
fields-objects
fields-objects are optional parameters of type JavaScript objects which are used to provide all those values to ga() function which are not already supplied via fields parameter:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘create’, ‘UA-12345-1’, ‘auto’, {‘name’:’testProperty’});
Here, {‘name’:’testProperty’} is a field object.
Google Analytics Command
Following is the syntax of GA command:
[trackerName.] [pluginName:] [methodName]
And it is used in the ga() function like the one below:
ga([GACommand], [fields], [fieldsObject]);
From the syntax above, we can conclude that GA command is one of the parameters passed to the ga() function and can be made up of: tracker name, plugin name, and command/plugin method.
The ga function is called a command queue function because it adds the commands it receives to a queue to delay execution and does not execute them immediately until the analytics.js library is fully loaded.
Note: If you supply a command to the ga() command queue function, which ga() does not recognize, it will simply ignore it.
Callback functions in Google Analytics
The ga() function can accept another function as an argument. For example:
ga(function_name);
The function which is passed an argument to another function is called the ‘callback function’.
The callback function is called once the method in which it is called completes execution.
So in case of Google Analytics, if you want to execute a function once the analytics.js library has fully loaded then pass that function as an argument to the ga() function: ga(function_name);
Another article you will find useful: Introduction to Google Analytics JavaScript Library – Analytics.js
In order to set up or troubleshoot any Google Analytics implementation, whether it is ecommerce tracking, cross-domain tracking, event tracking or enhanced ecommerce tracking, you need to understand how the ga() command queue function works.
Following is the syntax of the ga() command queue function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
trackerName is the name of the tracking object. (Optional parameter)
pluginName is the name of the analytics.js plugin. (Optional parameter)
methodName can be a command method or plugin method. (Required parameter)
fields are used based on the command method or plugin method being used. (Optional parameter)
fields-objects are JavaScript objects (Optional parameter)
These parameters are executed in the order in which they are supplied (from left to right) as soon as the analytics.js library is fully loaded.
Difference between arguments and parameters
‘argument’ is the actual value passed to a function when it is called. Whereas ‘parameter’ is a variable used in function declaration/definition to represent an argument.
For example:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
Here, trackerName, pluginName, methodName, fields and fields-objects are parameters of the function ga().
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
Here, ‘testProperty’, ‘linker’, ‘autolink’, [‘xyz.com’], ‘false’ and ‘true’ are arguments (actual values) of the function ga().
It is important that you remember the difference between arguments and parameters.
trackerName
trackerName is the name of a tracking object. It is one of the parameters of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
Tracking object (also known as tracker) is used to send hit data (pageviews, screenviews, events, etc) to a Google Analytics property.
There are two types of trackers:
#1 Default tracker
#2 User-defined tracker
The name of the default tracker is ‘t0’. But we generally do not refer to the default tracker by this name. We generally do not specify a tracker name when referring to the default tracker.
For example:
ga(‘send’, ‘pageview’); // send pageview data via default tracker.
However, if you are using multiple trackers or you do not want to use the default tracker, then you need to specify the name of the non-default tracker. For example:
ga(‘testProperty.send’, ‘pageview’); // send pageview data via the tracker named ‘testProperty’.
Since we do not always use multiple trackers, specifying tracker name is optional in a ‘ga’ function.
pluginName
pluginName is the name of an analytics.js plugin. It is one of the parameters of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘ec:addPromo’,{……………});
here ‘ec’ is the enhanced ecommerce plugin which is used to set up enhanced ecommerce tracking.
Another example:
ga(‘linker:autoLink’, [‘xyz.com’], false, true);
here ‘linker’ is a linker plugin which is used to set up cross-domain tracking.
If you are using multiple trackers or you do not want to use the default tracker, then you need to specify the name of the non-default tracker while using a plugin. For example:
ga(‘testProperty.ec:addPromo’,{……………});
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
here I am using the ‘ec’ and ‘linker’ plugins with a non-default tracker called ‘testProperty’.
Since we don’t always use plugins, specifying a plugin name is optional in a ‘ga’ function.
methodName
methodName can be a command method or plugin method.
Following are the examples of command methods:
#1 create – used to create a tracking object. Example: ga(‘create’, ‘UA-XXXX-Y’, ‘auto’);
#2 send – used to send hit data to Google Analytics. Example: ga(‘send’, ‘pageview’);
#3 set – used to set field names and values. Example: ga(set, ‘page’, ‘/home);
#4 require – used to set up analytics.js plugins. Example: ga(‘require’, ‘ec’);
#5 provide – used to provide analytics.js plugin and its methods for use with the ga() function.
#6 remove – used to remove a tracking object. Example: ga(‘myTracker.remove’);
Following are the examples of plugin methods:
#1 addImpression – used to measure product list view data in enhanced ecommerce tracking. Example: ga(‘ec:addImpression’,{…});
#2 addProduct – used to measure: product list clicks, product detail views, Product Add to Cart, Product Remove from Cart, Checkout Steps, Purchase and Refund data. Example: ga(‘ec:addProduct’,{…});
#3 SetAction – used to measure action data. Example: ga(‘ec:setAction’, ‘click’, {…});
#4 autoLink – used to define domains that need to be automatically cross-linked. Example: ga(‘linker:autoLink’, [‘destination1.com’, ‘destination2.com’]);
methodName is the only ‘required’ parameter of the ga() function:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example: ga(‘send’, ‘pageview’);
Here, ‘send’ is a command method used to send pageview hit.
Another example: ga(‘create’, ‘UA-12345-1’, ‘auto’);
Here, ‘create’ is a command method used to create a default tracker.
If you are using multiple trackers or you do not want to use default tracker, then you need to specify the name of the non-default tracker while using command or plugin methods.For example:
ga(‘testProperty.send’, ‘pageview’);
Here, ‘testProperty’ is a tracker name, ‘send’ is the command method used for ‘testProperty’
Another example,
ga(‘testProperty.linker:autoLink’, [‘xyz.com’], false, true);
Here, ‘testProperty’ is a tracker name, ‘linker’ is a plugin name and ‘autolink’ is the plugin method.
Note: you can’t use ‘testProperty.create’ as in:
ga(‘testProperty. create’, ‘UA-12345-1’, ‘auto’);
for creating a non-default tracker. It won’t work. The correct way to create a non-default tracker is mentioned below:
ga(‘create’, ‘UA-12345-1’, ‘auto’, {‘name’:’testProperty’});
Here, ‘create’ is a method used to create a new non-default tracker called ‘testProperty’
fields
fields are optional parameters of the ga() function which are used based on the command method or plugin method being used.
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘create’, ‘UA-12345-1’, ‘auto’);
Here, ‘UA-12345-1’ and ‘auto’ are the two fields of the ‘create’ command method.
Another example,
ga(‘linker:autoLink’, [‘xyz.com’], false, true);
Here, [‘xyz.com’], ‘false’ and ‘true are the various fields of the linker plugin method.
fields-objects
fields-objects are optional parameters of type JavaScript objects which are used to provide all those values to ga() function which are not already supplied via fields parameter:
ga([trackerName.] [pluginName:] [methodName], [fields], [fieldsObject]);
For example:
ga(‘create’, ‘UA-12345-1’, ‘auto’, {‘name’:’testProperty’});
Here, {‘name’:’testProperty’} is a field object.
Google Analytics Command
Following is the syntax of GA command:
[trackerName.] [pluginName:] [methodName]
And it is used in the ga() function like the one below:
ga([GACommand], [fields], [fieldsObject]);
From the syntax above, we can conclude that GA command is one of the parameters passed to the ga() function and can be made up of: tracker name, plugin name, and command/plugin method.
The ga function is called a command queue function because it adds the commands it receives to a queue to delay execution and does not execute them immediately until the analytics.js library is fully loaded.
Note: If you supply a command to the ga() command queue function, which ga() does not recognize, it will simply ignore it.
Callback functions in Google Analytics
The ga() function can accept another function as an argument. For example:
ga(function_name);
The function which is passed an argument to another function is called the ‘callback function’.
The callback function is called once the method in which it is called completes execution.
So in case of Google Analytics, if you want to execute a function once the analytics.js library has fully loaded then pass that function as an argument to the ga() function: ga(function_name);
Another article you will find useful: Introduction to Google Analytics JavaScript Library – Analytics.js
My best selling books on Digital Analytics and Conversion Optimization
Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.
Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.
Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.