nuxt-community / amp-module

AMP Module for Nuxt 2
https://codesandbox.io/s/github/nuxt-community/amp-module/
MIT License
204 stars 36 forks source link

Setup the GoogleAnalytics? #266

Closed drosanda closed 3 years ago

drosanda commented 3 years ago

Hi there,

how to setup properly the GoogleAnalytics using this amp-module? For first step I've put this code on layouts/default.vue

<amp-analytics type="googleanalytics" id="analytics1">
<script type="application/json">
{
  "vars": {
    "account": "UA-123456-X"
  },
  "triggers": {
    "trackPageview": {  // Trigger names can be any string. trackPageview is not a required name.
      "on": "visible",
      "request": "pageview"
    }
  }
}
</script>
</amp-analytics>

Terminal result

line 778, col 8: The script tag contains invalid JSON that cannot be parsed. (see undefined)

Chrome Console result:

log.js:255 [AmpAnalytics <unknown id>] Failed to parse <script> contents. Is it valid JSON?
[AmpAnalytics <unknown id>] No triggers were found in the config. No analytics data will be sent.

When I inspect element, the json are parsed into html entity.

<amp-analytics type="googleanalytics" id="analytics1" class="i-amphtml-element i-amphtml-layout-fixed i-amphtml-layout-size-defined i-amphtml-built i-amphtml-layout" i-amphtml-layout="fixed" aria-hidden="true" style="width: 1px; height: 1px;" hidden="">
  <script type="application/json">
      {&quot;vars&quot;:{&quot;account&quot;:&quot;UA-68369519-2&quot;},&quot;triggers&quot;:{&quot;trackPageview&quot;:{&quot;on&quot;:&quot;visible&quot;,&quot;request&quot;:&quot;pageview&quot;}}}
  </script>
</amp-analytics>
drosanda commented 3 years ago

I've try using amp-analytics remote config and its work.

Step to reproduce:

  1. Create file static/ga.json for remote configuration
    
    {
    "vars": {
    "account": "UA-123456-X"
    },
    "triggers": {
    "trackPageview": {
      "on": "visible",
      "request": "pageview"
    }
    }
    }
2. Put `amp-analytics type="googleanalytics" id="analytics1" :config="ga"` on `layouts/default.vue`
3. add script on `layouts/default.vue` 

export default { data() { return { ga: process.env.BASE_URL+'ga.json',

drosanda commented 3 years ago

I think this issue are fully solved now