Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Google Analytics with newer script

See original GitHub issue

I am trying to add Google Analytics to my site. I see how the documentation suggests to define a Google Analytics plugin, but Google told me to copy-paste code that isn’t the same as in that documentation. Here is the code Google told me to copy.

<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXX-1');

By looking at the previous documentation and looking at this documentation for Google’s data layer, I wrote this code.

export function googleAnalytics(gaCode: string) {
  let url = `'${gaCode}-1'`
  function addToDataLayer() {
    let newData = [{
      'js': new Date(),
      'config': `'UA-${gaCode}-1'`
    window.dataLayer = window.dataLayer || [];
  return function(): ConfigOverride {
    return {
      page: {
        scripts: [
          <script async src={url}></script>,

I think this is nearly correct, but I don’t know how to deal with window.dataLayer not being on defined on the type Window.


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

loreanvictorcommented, Aug 31, 2020

it can be. right now I push, test, if everything works, I will bump the version and push (typically via a commit called bump version), and then npm publish. this can be further automated of course, but its not much of a hassle (or particularly error-prone) to demand an automated workflow.

TysonMNcommented, Sep 6, 2020

I have written up all my thoughts here about why I think my code to communicate with Google Analytics from a CODEDOC site is correct.

Thanks for all your help @loreanvictor 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from analytics.js to gtag.js (Universal Analytics)
This guide walks you through the process of migrating an existing analytics.js Universal Analytics implementation to use gtag.js .
Read more >
[UA→GA4] Tips for switching from analytics.js to gtag.js
This article is for website owners who want to understand how analytics.js for a Universal Analytics (UA) property maps to the gtag.js for...
Read more >
How To Set Up Google Analytics With Next.JS Script Component
In this blog post, we'll go through how to set up Next.JS with Google Analytics using the latest versions (Next.JS 11 and Google...
Read more >
New vs Old Google Analytics script - Stack Overflow
Looks like some things aren't yet supported under "Universal Analytics", the new default.
Read more >
A Guide to Google Analytics Snippet Versions from 2005 to ...
In this blog post from MoreVisibility, discover all different kinds of code that are used to deploy Google Analytics.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found