Parcel: google gtag global snippet being changed to $s8z$var$gtag inside index.html

Created on 30 Jan 2019  路  5Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

Google gtag snippet is getting broken and rewritten as this:

<script>function $s8z$var$gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],$s8z$var$gtag("js",new Date),$s8z$var$gtag("config","UA-xxx-xx");</script>

馃帥 Configuration (.babelrc, package.json, cli command)

{
  "plugins": [
    "babel-plugin-styled-components"
  ]
}

馃 Expected Behavior

Inline snippet stays intact

馃槸 Current Behavior

The global is being changed to some garbage variables

馃拋 Possible Solution

馃敠 Context

package.json scripts (using the build one):

  "scripts": {
    "develop": "concurrently -r \"npm:type-check:watch\" \"npm:start\"",
    "start": "parcel serve ./src/index.html",
    "sw": "sw-precache --config=sw-precache-config.js",
    "build": "npm run type-check && rm -rf ./app/* && cross-env NODE_ENV=production parcel build ./src/index.html --experimental-scope-hoisting --public-url / --out-dir ./app --no-source-maps && npm run sw",
    "deploy": "npm run build && rm -f ./app/report.html && npm run deploy-now",
    "deploy-now": "sls syncToS3",
    "test": "jest",
    "type-check": "tsc --noEmit",
    "type-check:watch": "tsc --noEmit --watch",
    "postbuild": "react-snap"
  },

馃捇 Code Sample

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

    gtag('config', 'UA-xxx-xx');
  </script>

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.11.0
| Node | v11.8.0
| npm/Yarn | 6.7.0
| Operating System | windows 10

Bug

Most helpful comment

FWIW I've just done this on my end:

  <script>
    window.dataLayer = window.dataLayer || [];
    window.gtag = function gtag(){dataLayer.push(arguments);} // <-- note `window.gtag =`
    gtag('js', new Date());

    gtag('config', 'UA-xxx-xx');
  </script>

This ensures the gtag global is defined properly and Parcel doesn't get in the way.

All 5 comments

Parcel processes the code in every script tag, there is currently no way to disable that.
But the output should still work?

code expects to call gtag() thorough other part of the code, not $s8z$var$gtag (aka, not inside the index.html) https://developers.google.com/analytics/devguides/collection/gtagjs/events

That's unfortunately not possible at the moment. Closing as a duplicate of https://github.com/parcel-bundler/parcel/issues/2398

@pocesar I've just ran into this and came up with the following solution:

Add this to the HTML:

<meta type="google-analytics">

Run the following code as postprocess.js

const path = require('path');
const fs = require('fs').promises;

async function replace() {
    const placeholder = '<meta type="google-analytics">';
    const indexPath = path.resolve(__dirname, '../dist/index.html');
    const html = await fs.readFile(indexPath);
    const code = await fs.readFile(path.resolve(__dirname, './analytics.html'));
    const replaced = html.toString().replace(placeholder, code);
    fs.writeFile(indexPath, replaced);
}

replace();

Finally update my NPM build script using npm-run-all to run the above script when the build has finished.

FWIW I've just done this on my end:

  <script>
    window.dataLayer = window.dataLayer || [];
    window.gtag = function gtag(){dataLayer.push(arguments);} // <-- note `window.gtag =`
    gtag('js', new Date());

    gtag('config', 'UA-xxx-xx');
  </script>

This ensures the gtag global is defined properly and Parcel doesn't get in the way.

Was this page helpful?
0 / 5 - 0 ratings