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.

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.