Installing AnalyticsJS (CaliberMind snippet)

Installing AnalyticsJS on your site is the first step towards tracking page views and other actions on your website. This is the first step towards doing advanced attribution such as Return-on-ad-spend (ROAS) or Chain-based machine learning models.

There are two snippets typically used are: page and identify. These working together allows us to match user behavior to their identity when they provide it via a login or form submission (e.g. downloading an e-book.

Step 1 - Install "Page" snippet:

In the <head> of your site, place the below code on every page (replace the XXXXXXXXXXXX with your unique tracking script id provided by your CSM team).

<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Astronomer 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://")+"cdn.metarouter.io/a/v1/"+t+".js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";

analytics.load("XXXXXXXXXXXX");

analytics.page()
}}();
</script>

The snippet will load AnalyticsJS onto the page asynchronously, so it won’t affect the page load speed of your site. As soon as the snippet is running on your site, website data will start flowing into your CaliberMind tables.

Step 2 - Install "Identify" snippet:

Before the </body> closing tag place the below code -- this may need to be modified vary based your types of webforms or app, but the below works in 80% of cases:

<script type="text/javascript">
(function(){function on(e,t,n){if(e.addEventListener){e.addEventListener(t,n,false)}else if(e.attachEvent){e.attachEvent("on"+t,n)}}function fieldValuesFromForm(e){var t=e.querySelectorAll("input[type=text], input[type=email]");var n={};var r,a,i;for(var o=0;o<t.length;o++){r=t[o];var l=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;var u=String(r.value).trim();if(l.test(u)){n.email=u;break}}return n}function onFormSubmit(e){var t=e.target;var n=fieldValuesFromForm(t);if(!n.email)return;var r=e.defaultPrevented;e.preventDefault();var a;var i=function(){if(a)return;if(r)return;a=true;t.submit()};setTimeout(i,400);if(analytics){analytics.identify(n.email,{email:n.email})}else{if(console&&console.warn){console.warn("CaliberMind: analytics is not defined during submit")}}}on(window,"load",function(){var e=document.querySelectorAll("form");for(var t=0;t<e.length;t++){on(e[t],"submit",onFormSubmit)}})})();
</script>

For information on how to set up automatic identification through email forms, check out our knowledgebase article on the topic.


How did we do?