question-mark
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.

analytics script tag is duplicated after using the plugin

See original GitHub issue

When using google analytics script code, the prerendered html in the head looks like:

<script async="" src="https://www.google-analytics.com/analytics.js"></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','https://www.google-analytics.com/analytics.js','ga');

When the page is opened in a browser, it keeps the generated script tag, and the analytics code then when executed it adds another script tag, ending with double script tag

<script async="" src="https://www.google-analytics.com/analytics.js"></script>
<script async="" src="https://www.google-analytics.com/analytics.js"></script>

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
DamageESPcommented, May 1, 2019

In case this is useful, here’s what I’ve been doing to circunvent this:

I created a file called extraScripts.js, which exports a string with all the extra content I want to be added to each and every rendered page:

extraScripts.js

module.exports = `
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-X"></script>
<script type="text/javascript" src="https://repuso.com/widgets/grid.js" async></script>
<script type="text/javascript">
      var Tawk_API = Tawk_API || {},
        Tawk_LoadStart = new Date();
      (function () {
        var s1 = document.createElement("script"),
          s0 = document.getElementsByTagName("script")[0];
        s1.async = true;
        s1.src = 'https://embed.tawk.to/xxxxxx/default';
        s1.charset = 'UTF-8';
        s1.setAttribute('crossorigin', '*');
        s0.parentNode.insertBefore(s1, s0);
      })();
</script>
<script>
      window.dataLayer = window.dataLayer || []
      function gtag() {
        dataLayer.push(arguments)
      }
      gtag('js', new Date())
      gtag('config', 'UA-XXXXXXX-X')
</script>
`

Then, in webpack.prod.conf.js:

webpack.prod.conf.js

const extraScripts = require('./extraScripts.js')
/* ... */

new PrerenderSPAPlugin({
  // Required - The path to the webpack-outputted app to prerender.
  staticDir: path.join(__dirname, '../dist'),
  // Required - Routes to render.
  routes: ['route1', 'route2'],
  postProcess (context) {
    const bodyEnd = context.html.indexOf('</body>')
    context.html = context.html.substr(0, bodyEnd) + extraScripts + context.html.substr(bodyEnd)

    return context
  }
})
/* ... */

This produces the desired output for me. Of course you can modify the postProcess function to insert whatever string in whatever position in your final HTML.

4reactions
DamageESPcommented, Jul 20, 2018

All of my (async) scripts get duplicated upon prerendering. This screws all dynamically rendered content (external iframes, like Analytics, Repuso and Tawk.to in my case, don’t even render at all).

I’m surprised this is not addressed, and is deflected as “out of the scope” of the plugin or vague solutions are provided.

Hope we can shed some light into this and get a consistent solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate tag for analytics - WordPress.org
I'm using the plugin “Header Footer Code Manager” to add the scripts in the whole site. The page I need help with: [log...
Read more >
Find & Fix Duplicated Google Analytics Codes | GrowthSavvy
By copying and pasting the latest version of the analytics code into the section of your website's code. You can find this code...
Read more >
Same web property ID is tracked twice - Mixed Analytics
Hi Ana! The problem is that you have both on-site GA code (gtag.js) and Google Tag Manager running on your site. Since you're ......
Read more >
How can i remove duplicate Google Analytics code in my ...
I have installed Monster Insights plugin in my Wordpress site. I have also put GA code in theme options previously, but later I...
Read more >
How does duplicate Google Analytics tracking code affect a site?
The consequences of having two Google Analytics scripts, especially if they are pointing to the same UA-ID, is that you'll end up double ......
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found