Setup Google Universal Analytics

2022/03/052 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Setup Universal Analytics
  3. Setup Property
  4. Add Basic Information
  5. Setup Tracking Code
  6. Add Tracking Code to Web Page
  7. Verify the Tracking Code
  8. Conclusion

Introduction

This page shows an example of how to set up Universal Analytics for learning purposes. Please be aware that Google Universal Analytics will no longer process new data beginning July 1, 2023. You'll need to switch to a Google Analytics 4 Property.

Setup Universal Analytics

If you're new to Google Analytics, you'll need to set up an account. Once you have a Google Analytics account, you can create a property as the screen shows below.

Again, this blog is for learning purposes only, so we choose the Universal Analytics property, but most likely you'll need to use the GA4 property.

Setup Property

Setup Property

Add Basic Information

Add Basic Information

Setup Tracking Code

At this point, you should be getting the Google Tracking code snippet. You'll need to add this to your website so the site can start tracking activity.

Setup Tracking Code

Add Tracking Code to Web Page

As described in the Setup Tracking Code section, you'll need to copy the Google Tracking Code to any page on your site that needs tracking.

The following is sample code showing how to add a Google Tag Manager script to Next.js.

import Script from 'next/script'
export default function Nextra({ Component, pageProps }) {
  return (
    <>
      <Script
            strategy="afterInteractive"
            src='https://www.googletagmanager.com/gtag/js?id=UA-{Your Property ID}' />
      <Script
        strategy="afterInteractive"
        dangerouslySetInnerHTML={{
          __html: `
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', 'UA-{Your Property ID}');
          `,
        }}
      />
    </>
  )
}

Verify the Tracking Code

After adding the above tracking code to your site, go back to Google Analytics and check if there are any records.

The above code sends a pageview to Google Analytics.

gtag('config', '{your tracking ID}');

Conclusion

In this blog, we walked through the simple steps to set up Google Universal Analytics on your website. While Universal Analytics is being sunset in favor of GA4, understanding the setup process remains useful for migration planning and maintaining legacy tracking configurations.