Analytics.js – The Google Analytics JavaScript Library – Tutorial

This article is on analytics.js JavaScript Library. If you are looking for an article on gtag.js JavaScript Library then check out the following article: gtag.js – Google Tag in Google Analytics 4 and beyond.

In order to implement ecommerce tracking, cross-domain tracking or enhanced ecommerce tracking on your website, you need to customize the Google Analytics tracking code.

To achieve this objective, without breaking the GA tracking code, you need to understand what analytics.js library is. Analytics.js is a JavaScript library that is used to measure users’ interactions with your website. Whenever we refer to Google Analytics, we are actually referring to this library.

You can add analytics.js to your website by placing the following JavaScript code (also known as Google Analytics Tracking Code) on each page of your website, immediately before the closing head tag (</head>):

ga tracking code

The ‘UA-XXXXX-Y’ in the code above is known as the tracking id (also known as property id).

This id (which is actually a string) tells Google Analytics (aka analytics.js library) which account and property to send data to.

Following is an example of a tracking id:

UA-12345-21

The tracking id is made up of two parts:

#1 Account number
#2 Property number

account property number

Note: You can view analytics.js JavaScript library by visiting this URL: https://www.google-analytics.com/analytics.js

Finding Tracking ID for your website

Though the tracking id is already included in your Google Analytics tracking code, it is still important to know, what a tracking id is and how to find it, for your website.

This is because you would need this tracking id while using the Google Analytics API, Measurement Protocol (which is generally used to collect data from devices other than desktops, laptops, tablets and mobiles) or Google Analytics SDK (which is used to collect data from mobile apps).

To find your tracking ID, go the ‘Admin’ section of your account and then click on the ‘Tracking Code’ link under ‘Property’ > ‘Tracking info’:

tracking code
tracking id2

What not to modify in the GA Tracking Code

The highlighted code below is used to initialize the ga() command queue function and to load the analytics.js library:

what not to modify

Avoid modifying this part of the code, unless you know exactly what you are doing. Whatever custom code you wish to add, add it after the highlighted code.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

Preloading Google Analytics Tracking Code

Google provides an alternative asynchronous version of its tracking code which allows modern web browsers to pre-load the tracking code:

<!– Google Analytics –>

<script>

window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;

ga(‘create’, ‘UA-XXXXX-Y‘, ‘auto’);

ga(‘send’, ‘pageview’);

</script>

<script async src=’//www.google-analytics.com/analytics.js’></script>

<!– End Google Analytics –>


By using this tracking code, you can load Google Analytics a bit faster on your web pages. The downside of using this script is that it turns into synchronous loading on older web and mobile browsers such as IE 9.

Use this script if the majority of your website visitors use modern web browsers.

Frequently Asked Questions About Analytics.js – The Google Analytics JavaScript Library – Tutorial

What is Analytics.js

Analytics.js is a JavaScript library that is used to measure users’ interactions with your website. Whenever we refer to Google Analytics, we are actually referring to this library.

How do I add Analytics.js to my website?

You can add analytics.js to your website by placing the Google Analytics tracking code on each page of your website, immediately before the closing head tag (</head>).

To find your tracking ID, go the ‘Admin’ section of your account and then click on the ‘Tracking Code’ link under ‘Property’ > ‘Tracking info’:

What is the tracking ID?

The ‘UA-XXXXX-Y’ part in the Google Analytics tracking code is known as the tracking id (also known as property id).

This id (which is actually a string) tells Google Analytics (aka analytics.js library) which account and property to send data to.

Following is an example of a tracking id: UA-12345-21

The tracking id is made up of two parts:
#1 Account number
#2 Property number
account property number

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.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade