info |
Note MoEngage highly recommends using the optional step for MoEngage Web SDK integration. |
Track Event
Tracking events records the user actions and the action properties. A single user action is recorded with every track_event call. MoEngage recommends that you make your event names human-readable so that everyone in your team can know what they mean instantly.
You can track an event using track_event with the event name and the event characteristics such as attributes or properties.
Moengage.track_event("EVENT_NAME_1"); // This event has no attributes
// Track events with additional attributes
Moengage.track_event("EVENT_NAME_2", {
"attribute_1": "value_1", // string value
"attribute_2": 2, // numeric value
"attribute_3": 3.4, // numeric value
"attribute_4": new Date(2017, 0, 31), // datetime value. Example value represents 31 January, 2017.
});
Add all the additional information which you think would be useful for segmentation while creating campaigns. For example, the following code tracks a purchase event of a product. We are including attributes like amount, quantity, category, which describe the event we are tracking.
Moengage.track_event("Purchase", {
"quantity":2,
"product_name":"Ipad mini",
"price": 599.99,
"currency": "USD"
});
warning |
Critical
|
Event tracking using Google Tag Manager (GTM)
Use the described code in the Custom HTML Tag
inside GTM. This Tag should be fired once per event and triggered on the elements where you wish to track website events. The event attributes can be picked up from GTM Data Layer.
<script>
Moengage.track_event("Purchase", {"quantity":2, "product_name":"Ipad mini", "price": 599.99, "currency": "USD" });
</script>
Non-Interactive Event
Events that do not affect the session duration calculation in MoEngage Analytics are marked as Non-Interactive events.
The following are considered non-interactive events:
- Do not start a new session, even when the website is in the foreground
- Do not extend the session
- Do not have information on source and session
For example, events that are tracked when the website is in the background, are not initiated by users and hence are marked as non-interactive.
To mark an event as non-interactive send moe_non_interactive: 1
while tracking the event as described:
Moengage.track_event("Content Refreshed", {"NewsCategory": "Politics", "moe_non_interactive": 1})
Callbacks on events tracked by SDK
There are few events which are tracked by SDK automatically for you and if you want to get those, then you can listen to the events as follows:
window.addEventListener('MOE_AUTOMATED_EVENTS', function (event) {
switch(event.detail.name) {
case 'MOE_WEB_UNSUBSCRIBED':
console.log(event.detail.data) // it contains all the attributes of the event
break;
case 'EVENT_ACTION_WEB_SESSION_START':
console.log(event.detail.data) // it contains all the attributes of the event
break;
……
}
});
List of events
- MOE_WEB_UNSUBSCRIBED
- EVENT_ACTION_WEB_SESSION_START
- MOE_PAGE_VIEWED
- MOE_WEB_OPTIN_BANNER_LOAD
- MOE_WEB_OPTIN_CLOSED
- MOE_WEB_OPTIN_ACCEPTED
- MOE_USER_SUBSCRIBED
- MOE_OPT_IN_SHOWN
- MOE_OPT_IN_ALLOWED
- MOE_OPT_IN_DISMISSED
- MOE_OPT_IN_BLOCKED
- MOE_LOGOUT
- MOE_ONSITE_MESSAGE_CLICKED
- MOE_ONSITE_MESSAGE_SHOWN
- MOE_ONSITE_MESSAGE_DISMISSED
- MOE_ONSITE_MESSAGE_AUTO_DISMISS
Troubleshooting
Event is not tracked on redirection when I am using window.open(..)
In case you are redirecting to another page using window.open
method and also tracking some event(s) on redirection then you need to wrap the window.open
in a setTimeout
. eg-
setTimeout(() => window.open(<redirection_link>), 0);
Events are not getting tracked when the page URL is very big
URL is tracked by default in all the events as an attribute. But the maximum length of the URL supported is 512 character. If the URL length exceeds that, then the whole events will be dropped.
So make sure the URL length does not exceed 512 characters
Page Viewed event is not getting tracked on back/forward navigation
Your website might be eligible for bfcache. Back/forward cache (or bfcache) is a browser optimization that enables instant back and forward navigation. bfcache is an in-memory cache that stores a complete snapshot of a page (including the JavaScript heap) as the user is navigating away. Read more about bfcache here
Thus the Moengage SDK does not re-initialises again and hence the page viewed events are not tracked.
To include bfcache restores in your pageview count, set listeners for the pageshow
event and check the persisted
property.
window.addEventListener('pageshow', (event) = {
// Send another pageview if the page is restored from bfcache.
if (event.persisted) {
Moengage.track_page_view();
}
});
To contact the MoEngage Support team, you can raise a ticket through the Support Web Form within the MoEngage dashboard. For more information, refer here.