Friday, April 1, 2016

Hosting multiple client side telemetry Application Insight endpoints on same domain


We have a website where one division in the company controls and builds the main content of our website, and another division deals with the online ordering cart system.  We each host our websites on separate servers, but they are served up under the same domain name and we share cookies.  The routing to the correct server is all handled up-stream by our network team, so it is seamless to the developers and our users.

The division that handles the online cart has already installed AppInsights and have been collecting client-side javascript telemetry data for a while now.  I work on the other content side of the website and we want to collect our own telemetry.

Looking at the code, the tracking logic and config is loaded and saved to a global variable: window.appInsights.  That variable is reloaded every time a page is requested and the trackPageView() is called when the page loads.

If we were to install the ApplicationInsights client code in our part of the website, which is the typical entry point into our website, then the window.appInsights object will be initialized by us and use our instrumentationKey.

To prevent our collection from replacing theirs, we updated the client code to set our own global variable.

OLD:
<script type="text/javascript">
  var appInsights = window.appInsights || function (config) { 
  ... 
  window.appInsights = appInsights; 
  appInsights.trackPageView(); 
</script>

NEW:
<script type="text/javascript">
  var appInsights = window.appInsightsSitecore || function (config) { 
  ... 
  window.appInsightsSitecore = appInsights; 
  appInsights.trackPageView(); 
</script>

No comments:

Post a Comment