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.

4
  • Can you append it at runtime with something like this? stackoverflow.com/questions/9413737/… Commented May 20, 2019 at 0: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. Commented May 20, 2019 at 5: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. Commented May 20, 2019 at 16:29
  • As we are releasing via pipeline, what I've done is update the insights key using a powershell step. Commented May 21, 2019 at 8:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.