Analytics.js for Web

Overview

This guide will cover how to implement the Analytics.js SDK for MetaRouter and begin sending events into your MetaRouter cluster from your website. This is the easiest MetaRouter install and can generally be completed in minutes, assuming you have already created your Data Infrastructure.

Installing the SDK

Once the snippet is placed on your website within the <head> tag, you will be ready to trigger events from your website:

<script type="text/javascript">
  !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("MetaRouter snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"YOUR_CDN_URL"+t+".js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
  analytics.load("YOUR_WRITE_KEY");
  analytics.page()
  }}();
</script>

Note that there are two edits you need to make to the above snippet:

  • Your CDN URL, which is where the analytics.js file the snippet calls to is located. You will need to determine your preferred DNS settings prior to setting the CDN URL and adding the snippet. Please see this guide for more information.
  • Your writeKey, which will be determined as a part of your MetaRouter cluster setup.

Default Page Events

The snippet above includes the analytics.page() function, which will automatically trigger Page calls. You can remove this function from the snippet if you do not want to trigger Page calls on every webpage loaded on your website. We recommend leaving this function in the snippet in most cases.

Considerations for Self-Hosting CDN files
We recommend hosting the Analytics.js file within your own CDN in order to receive the maximum 1st Party benefits from our platform.

You will be provided a custom-built version of this SDK that is GZIP compressed. To maintain this compression, which minimizes the page load time, you will need to set the following Metadata on the SDK within your CDN if it does not do so automatically.

On the Upload window, add the following Metadata

Type

Key

Value

System Defined

Content-Encoding

gzip

System Defined

Content-Type

application/javascript

Analytics.js for Web Event Methods

With the following events, you can track almost any kind of action being taken and consistently identify the users on your website. Check out each event type below to learn more about when to trigger them and what to include with each event.

Analytics.js & Cookies

The Analytics.js library places identifiers in the form of first-party cookies on the browser that are persisted between user sessions. The library will always create the ajs_anonymous_id cookie if that cookie is not already present, and will create the ajs_user_id cookie only if an identify call is made. If one of these cookies is cleared, the value is also saved as a backup on the browser's Local Storage, allowing for the value to be restored if the user deletes their cookies. Both cookies will expire after one year, unless browser restrictions cause cookies to expire within a shorter timeframe.