0

Is it possible to use Application Insights with Angular JS 1.5.x?

The snippet from this page seems to require a static key, but we need to specify this at run time, per environment:

<script type="text/javascript">
var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){function n(e){i[e]=function(){var n=arguments;i.queue.push(function(){i[e].apply(i,n)})}}var i={config:e};i.initialize=!0;var a=document,t=window;setTimeout(function(){var n=a.createElement("script");n.src=e.url||"https://az416426.vo.msecnd.net/next/ai.2.min.js",a.getElementsByTagName("script")[0].parentNode.appendChild(n)});try{i.cookie=a.cookie}catch(e){}i.queue=[],i.version=2;for(var r=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];r.length;)n("track"+r.pop());n("startTrackPage"),n("stopTrackPage");var o="Track"+r[0];if(n("start"+o),n("stop"+o),!(!0===e.disableExceptionTracking||e.extensionConfig&&e.extensionConfig.ApplicationInsightsAnalytics&&!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){n("_"+(r="onerror"));var s=t[r];t[r]=function(e,n,a,t,o){var c=s&&s(e,n,a,t,o);return!0!==c&&i["_"+r]({message:e,url:n,lineNumber:a,columnNumber:t,error:o}),c},e.autoExceptionInstrumented=!0}return i}
(
    {instrumentationKey:"INSTRUMENTATION_KEY"}
);
window[aiName]=aisdk,aisdk.queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
</script>

Articles like this one seem useful (unless you're familiar with TypeScript) and we're not using TS, so porting is a problem.

Matt W
  • 11,753
  • 25
  • 118
  • 215
  • Can you append it at runtime with something like this? https://stackoverflow.com/questions/9413737/how-to-append-script-script-in-javascript – Mark Wolff May 20 '19 at 00:17
  • I have tried something similar to this. It would appear that if the script is started after AngularJS starts up it will not pick up anything from Angular and thus nothing is logged. – Matt W May 20 '19 at 05:27
  • 1
    You can alternatively just change the instrumentation key config string at runtime whenever you want. `window.appInsights.config.instrumentationKey = "something-else"`. If the ikey is unknown until this point, nothing would be logged/saved until it is. You could instead bake up your own queue abstraction and load in app insights in some HOC. – Mark Wolff May 20 '19 at 16:29
  • As we are releasing via pipeline, what I've done is update the insights key using a powershell step. – Matt W May 21 '19 at 08:37

0 Answers0