Search code examples
google-analyticsamp-html

AMP pages adding Google Anayltics AmpAnalytics Errors


I am following simple guide by https://developers.google.com/analytics/devguides/collection/amp-analytics/

Trying to add Pageviews GA code, but facing these two errors in console:

amp-analytics-0.1.max.js:243 AmpAnalytics analytics1 Analytics config could not be parsed. Is it in a valid JSON format? SyntaxError: Unexpected token /

AmpAnalytics analytics1 No triggers were found in the config. No analytics data will be sent.

Anyone have faced these errors and knows the fix, I have followed their docs as instructed but still facing above two issues.


Solution

  • I got it resolved, make sure you remove those comments from the sample code and errors will be no more.

    i.e. you remove comments from following code i.e. delete "// Replace with your property ID."

    and // Trigger names can be any string. trackPageview is not a required name.

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

    So it i will become this :

    <amp-analytics type="googleanalytics" id="analytics1">
    <script type="application/json">
    {
      "vars": {
        "account": "UA-XXXXX-Y" 
      },
      "triggers": {
        "trackPageview": { 
          "on": "visible",
          "request": "pageview"
        }
      }
    }
    </script>
    </amp-analytics>