knucklehead
knucklehead

Reputation: 11

Global Site Tag (gtag) for AMP Not Firing

I am have been trying for a couple days now to implement Google Analytics using gtag to my AMP page with no luck.

I have poured through the Google Developer Docs here: https://developers.google.com/gtagjs/devguide/amp and have followed them to no avail.

I have also gone through the "amp-analytics" docs here: https://www.ampproject.org/docs/reference/components/amp-analytics to see if maybe something had been missed but could not find anything.

Below I have copied how I bring in the AMP Project, the AMP Analytics component and my AMP Analytics json script:

<head>
<meta charset="utf-8">
<title>Untitled Document</title>

<script async src="https://cdn.ampproject.org/v0.js"></script>

<script async custom-element="amp-analytics"
src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
</head>

<body>
<amp-analytics type="gtag" data-credentials="include">
    <script type="application/json">
    {
        "vars": {
            "gtag_id": "<UA-XXXXXXX-XX>",
            "config": {
                "<UA-XXXXXXX-XX>": {
                "groups": "default",
                "linker": { "domains": ["site1.com", "site2.com"] }
                }
            }
        },
        "triggers": {
            "link": {
                "selector": "#specific-link-id",
                "on": "click",
                "vars": {
                    "event_category": "link_clicks",
                    "event_action": "click",
                    "event_label": "specific_link"
                }
            },
            "button": {
                "selector": "#specific-button-id",
                "on": "click",
                "vars": {
                    "eventCategory": "bttn_clicks",
                    "eventAction": "click",
                    "eventLabel": "specific_button"
                }
            }
        }
    }
    </script>
</amp-analytics>
</body>

When running the above json script on my AMP page I receive the following error from DevTools: AMP Error from Chrome DevTools but according to the Google Developer Docs for gtag I have everything setup correctly.

Am I missing anything? Or should I just switch to type="googleanalytics"?

Thank you in advance for any and all assistance!

Upvotes: 1

Views: 1109

Answers (1)

zikas
zikas

Reputation: 21

Couple of things here.

First, "<UA-XXXXXXX-XX>" must be a valid tracking ID. This will be enough to send page_view event.

Second, if you're using triggers, you need to provide event_name in trigger vars.

"triggers": {
            "link": {
                "selector": "#specific-link-id",
                "on": "click",
                "vars": {
                    "event_name": "my_event",
                    "event_category": "link_clicks",
                    "event_action": "click",
                    "event_label": "specific_link"
                }
            },
...

You can use one of the standard events defined here https://developers.google.com/gtagjs/reference/event or use any custom event name (see https://developers.google.com/gtagjs/reference/api#event for more details)

Upvotes: 2

Related Questions