Introduction to Google Analytics Commands

In order to set up / 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]);

Here,

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 parameter 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 tracker name, when referring to a 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 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 parameter 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 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 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 creating 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 parameter 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 receive to a queue to delay execution and does not execute them immediately, until the analytics.js library is fully loaded.

NoteIf 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 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

Announcement about my books

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 Beyond
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.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Content is protected !!