Paul Dufour
Paul Dufour

Reputation: 11

How to manually fire an amp-analytics call?

Hi I was wondering if there was a way to manually fire an analytics event. We are planning to use AMP to display desktop pages as well, and we have some events that don't fit into the triggers available here: https://www.ampproject.org/docs/reference/extended/amp-analytics.html#triggers.

For instance, we have an infinite scroll, and we'd like to fire an event when the user reaches the second page of it. We have some custom JS so I understand it's breaking the AMP spec, but we still have an amp-compliant page without the custom JS, and therefore without the infinite scroll.

I looked around a bit on the window.AMP object but couldn't find anything of use. Seems like the analytics stuff may be in a private object somewhere.

Upvotes: 0

Views: 780

Answers (1)

Android Enthusiast
Android Enthusiast

Reputation: 4950

Try to use Event tracking

Based on the Official Google Documentation event tracking are user interactions with content that can be tracked independently from a web page or a screen load.

Event hits can be sent by setting the trigger request value to event and setting the required event category and action fields.

The following example uses the selector attribute of the trigger to send an event when a particular element is clicked:

<amp-analytics type="googleanalytics" id="analytics3">
<script type="application/json">
{
"vars": {
"account": "UA-XXXXX-Y"
},
"triggers": {
"trackClickOnHeader" : {
  "on": "click",
  "selector": "#header",
  "request": "event",
  "vars": {
    "eventCategory": "ui-components",
    "eventAction": "header-click"
  }
}
}
}

For more information about Events, click here: https://support.google.com/analytics/answer/1033068

Upvotes: 1

Related Questions