Skip to main content
MoEngage AMP event analytics plugin helps in track user attributes and events, and run third-party javascript. MoEngage AMP event analytics plugin ensures to address the differences in AMP pages and HTML page restriction for tracking user attributes and events. The MoEngage AMP event analytics plugin is different from the AMP analytics module. To add event tracking and user attribute tracking to your AMP pages follow these steps-

Add AMP Analytics Script

Ensure to include the script in all of your AMP pages in the <head> section of your .amp file where you want to use AMP analytics and track user attributes and events.

Add Anywhere in your HTML

NoteEnsure to replace Your_Workspace_ID with the actual Workspace Id from MoEngage Dashboard -> Settings -> App -> General SettingsFor dataCenter, please contact our support team to know more.
NoteTo redirect data to test environment, append ‘_DEBUG’ to appId. For example, if your appId is 
YOUR_WORKSPACE_ID then for test environment, it would be YOUR_WORKSPACE_ID_DEBUG

Tracking Users

All the users visiting your AMP pages will be tracked automatically once you followed the above steps.
But these users will be anonymous users by default.
However, if any user of your website who visited your normal HTML pages earlier and has not deleted their cookies, will be treated as the same user in AMP pages also.

Tracking Events

Page Viewed event is tracked by default if you followed the above steps.
However with AMP framework limitations on event tracking, only a few kinds of events can be tracked such as Page Viewed, Element Clicked, Page Scroll.
For more information on the list of events, refer to AMP Analytics Examples.

Example 1: Element Click Event

You can track a Click Event when an HTML element with id test is clicked as described:
 

Example 2: Page Scroll Event

The scroll event needs scrollSpec object, that contains verticalBoundaries and horizontalBoundaries. At least one of the two properties is required for a scroll event to fire. The values for both of the properties should be arrays of numbers containing the boundaries on which a scroll event is generated. For instance, in the following code snippet, the scroll event will be fired when the page is scrolled vertically by 25%, 50% and 90%. The attributes sent here is scrolledUpto which holds an inbuilt variable scrollTop that provides the number of pixels that the user has scrolled from the top. For more information about the list of all supported variables, refer to Supported Variables.

Example 3: Form Submit Event

In the above example, we are setting the id of the form as the selector value (#testForm). Inside “extraUrlParams”, “e” is the event name and “a” contains the key and the value which we want to track for this event.

Tracking User Attributes

ImportantIf any user attribute is configured as an identity for your account, then track it as part of login instead of simply tracking it as a user attribute as it is shown in this section. Refer to this document to learn more.
You can track user attributes using the “EVENT_ACTION_USER_ATTRIBUTE” event. For example, you have this button on your website:
Then, track the first name (considering it to be the user’s first name) like this:
Similarly, to track the user’s email upon clicking the below button
In the same way, you can track other pre-defined user attributes
Here, USER_ATTRIBUTE_NAME has to be replaced with one of the following
To track a custom user attribute, in place of USER_ATTRIBUTE_NAME you have to use your own custom attribute name. For example-
In the above example, we are tracking a custom attribute named “colors”. This attribute’s value is an array- ["blue","green","red"]

Tracking User Login and Logout

Previous way of logging-in users has been changed. The below method follows the User Identity Resolution feature of MoEngage. Learn More. Ensure log in and log out of users are implemented correctly during the visit to your website and users are authenticated. 
ImportantIf the user log in and log out is not handled correctly, user data may get corrupted. Refer this section for more details.

Track Login

Here, we want to track ID “someUniqueId” and Email “emailValue@emailDomain.com” as the identities for the user.
ImportantIf you are setting the ID, make sure to add/update its value in both identifiers -> moe_user_id as well as inside identifiers -> user_identities
Here, uid is ID and u_em is Email (Standard) attributes. Please refer to the below table for key names which need to be used to set standard user attributes as identities. If we want to identify the user with just mobile number (for example)-
NoteHere, we do not have to send the value of mobile number in identifiers -> moe_user_id as Mobile and ID are two different attributes of a user. Only while adding/updating ID, the value of the ID has to be sent in moe_user_id as well as in user_identities.
ImportantAfter tracking the login, all further attributes and events tracking should have the “identifiers” object defined inside the tracking code (with the “moe_user_id” and/or “user_identities”) as above, until logout event is performed. Otherwise, that attribute or event tracking will not be associated with this logged-in user in your MoEngage dashboard.
For example, if we want to track an event after identities have been set for the user-

Track Identities Update

When updating the value of an identity, send the changed identities in identifiers -> previous_identities. For example, let’s assume you first set ID and Email as identities of the user.
Now, let’s say you want to update the ID of the user.
NoteIn the above example, we updated ID. You can update more than one identities but you have to mandatorily send all the changing identities in previous_identities.

Track Logout

ImportantAfter performing the above Moengage logout event, do NOT send “identifiers” with any further tracking code. Because the user had logged-out and further attribute/event tracking must not be associated with this user.