What is Google Analytics JS?

Cody Schneider7 min read

Ever bumped into a file named ga.js in your website’s code and wondered what secret mission it's on? That small JavaScript file was the original engine behind Google Analytics, the tool that once powered web traffic measurement across the internet. This article breaks down what a Google Analytics JS file is, traces its evolution through the years, and clarifies which version you should be using today.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What Exactly is Google Analytics JS (ga.js)?

In short, ga.js was a JavaScript tracking library for Classic Google Analytics - the first major version of the platform. Think of it as a diligent reporter placed on your website. Its job was to observe visitors, note their actions, and send a detailed report back to Google's servers. Every time a user landed on a page, this script would spring into action.

The ga.js library collected essential user data such as:

  • Which pages were visited (pageviews).
  • How long visitors stayed on a page.
  • The website they came from (referral source).
  • The visitor's geographic location, browser, and operating system.

By placing this single file within your website’s HTML, you could unlock powerful insights into your audience's behavior. For its time, it was a game-changer, giving marketers and business owners a clear view of their website's performance without needing a data science degree.

How Did It Work? The Synchronous Way

The original Classic Analytics snippet using ga.js looked something like this. You’d place it just before the closing </head> tag in your site’s HTML.

<script type="text/javascript">
  var _gaq = _gaq || [],
  _gaq.push(['_setAccount', 'UA-XXXXX-X']),
  _gaq.push(['_trackPageview']),
  (function() {
    var ga = document.createElement('script'), ga.type = 'text/javascript', ga.async = true,
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + '.google-analytics.com/ga.js',
    var s = document.getElementsByTagName('script')[0], s.parentNode.insertBefore(ga, s),
  })(),
</script>

A key characteristic of ga.js was that it loaded synchronously. In simple terms, this means the browser had to load and execute this script before it could move on to loading other elements on the page. In some cases, if the script took time to load, it could slow down the page's overall rendering time - a major drawback from a user experience and SEO perspective.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Times Change: The Evolution of the Google Analytics Script

The digital world moves fast, and user behavior changes along with it. The simple pageview metric that was once king became just a small piece of a much bigger puzzle. To keep up, Google’s tracking technology had to evolve. The journey from ga.js to today's standard happened in a few key stages.

Stage 1: ga.js (Classic Analytics)

This was the original script that set the standard for web analytics. Its session-based model focused on pageviews and basic user interactions. Simple, effective, but it couldn't keep pace with the rise of mobile devices and more complex user journeys.

Stage 2: analytics.js (Universal Analytics)

Next came Universal Analytics (UA), and with it, the analytics.js library. This was a major upgrade designed for a world where users logged in from their phones, tablets, and desktops. The new script introduced User ID tracking, allowing businesses to analyze user behavior across different devices and sessions.

More importantly, analytics.js loaded asynchronously. The browser could load this script in the background while the rest of the page's content continued to load. This small change made a big impact on a site's loading speed and performance.

The Universal Analytics snippet looked like this:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r,i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date(),a=s.createElement(o),
m=s.getElementsByTagName(o)[0],a.async=1,a.src=g,m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'),

ga('create', 'UA-XXXXX-X', 'auto'),
ga('send', 'pageview'),
</script>
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Stage 3: gtag.js (The Global Site Tag and GA4)

The most recent and current standard is the Global Site Tag, or gtag.js. This script powers Google Analytics 4, the latest generation of Google's analytics platform. Unlike its predecessors, which were built solely for Google Analytics, gtag.js is a unified tagging library. This means you can use the same snippet to send data to multiple Google products, like Google Ads and Google Analytics, without cluttering your code.

The biggest shift with GA4 and gtag.js is the move to an event-based measurement model. Instead of focusing on sessions and pageviews, this model treats every user interaction - a click, a video play, a form submission, a page view - as an individual event. This flexible framework is much better suited for tracking today’s complex user journeys across both websites and mobile apps.

Here’s the standard gtag.js snippet for GA4:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [],
  function gtag(){dataLayer.push(arguments),}
  gtag('js', new Date()),

  gtag('config', 'G-XXXXXXXXXX'),
</script>

Why Did Google Analytics JS Change?

The transition from ga.js to gtag.js wasn't just for a change of scenery. It reflects fundamental shifts in how we use the internet and what businesses need to measure.

  1. Performance: The synchronous loading of ga.js was a performance bottleneck. The switch to asynchronous loading with analytics.js and gtag.js ensures that analytics tracking doesn't get in the way of a fast user experience.
  2. Cross-Device Behavior: Users no longer browse the web from a single device. The User ID feature in Universal Analytics was the first step toward connecting the dots, and GA4's data model refines this multi-platform view even further.
  3. Simplicity & Management: Managing multiple tracking codes for different Google products was messy. gtag.js centralized everything, creating a single, streamlined framework for all Google-related tracking.
  4. Flexibility: The session-based model was too rigid for modern websites and apps. GA4’s event-based model allows you to define and track literally any interaction that matters to your business, giving you a far more customized and relevant picture of engagement.
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Should You Still Be Using ga.js?

Absolutely not. If you find a ga.js script still running on your website, it's time for an urgent update. Not only is the tracking library outdated, but Classic Google Analytics, the service it reported to, was shut down years ago. Even Universal Analytics and its companion script, analytics.js, were officially sunsetted by Google in July 2023.

Any data collection happening through these older scripts is no longer being processed. To get accurate website analytics, you must be using Google Analytics 4 with the gtag.js tracking code. If not, you are effectively flying blind.

The Best Way to Implement Google Analytics Today

While you can still paste the gtag.js snippet directly into your website’s HTML, the modern best practice is to use Google Tag Manager (GTM).

GTM is a free tool that acts as a middleman between your website and your analytics tools. Instead of adding every tracking script in your site’s code, you install the GTM container once. From there, you can add, edit, and manage all your tags - like the Google Analytics 4 tag - from an easy-to-use web interface, no coding required.

Using Google Tag Manager offers several major benefits:

  • Empowers marketers: You no longer have to ask a developer for help every time you want to add a new tracking pixel or adjust an existing tag.
  • Improved site performance: Because GTM loads its tags asynchronously, it doesn't slow down your website.
  • Better organization: It helps you control all your analytics, marketing, and advertising tags in one central, organized hub.
  • Built-in testing tools: GTM's preview mode lets you test your tags and make sure they're firing correctly before you publish them to your live site.

Final Thoughts

The ga.js script was the original key that unlocked web analytics for millions, but technology has moved on. The lineage from ga.js to analytics.js to today's gtag.js tells a story of an internet that has grown more complex, powerful, and user-centric. If you see an older script on your site, it’s a clear signal to migrate to a modern Google Analytics 4 and Google Tag Manager setup.

Once you have your GA4 data flowing, the next challenge is turning it into insights without spending all day in reporting tabs. At Graphed, we connect directly to your Google Analytics, Google Ads, Shopify, and other marketing platforms in one click. Just ask a question in plain English, and we’ll instantly build the real-time dashboard or report you need, so you can spend less time wrangling data and more time acting on it.

Related Articles