Search code examples
javascriptazure-application-insightsappinsights

AppInsight TrackDependency() from javascript is not working


I am trying to log into dependency table of appinsight, I am using frontend code javascript. Below is the snippet. It executes fine but I don't see any entry in dependency table of appinsight.

var appInsights = window.appInsights || function (a){function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
     }({
         instrumentationKey: '@Model.InstrumentationKey'
     });



    window.appInsights = appInsights;
    appInsights.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL"});

Is there anything wrong in above code?


Solution

  • It should be incorrect use of trackDependency method. You can refer to this doc for the definition of trackDependency(in the page, search trackDependency).

    And here is another link about how to use trackDependency method in JavaScript.

    Here is a sample code, and works at my side(you can modify it to meet your need):

    <script type="text/javascript">
    var appInsights=window.appInsights||function(config){
        function s(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } } var t = { config: config }, r = document, f = window, e = "script", o = r.createElement(e), i, u; for (o.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", r.getElementsByTagName(e)[0].parentNode.appendChild(o), t.cookie = r.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace","Dependency"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t
    }({
        instrumentationKey:"the key"
    });
    window.appInsights = appInsights;
    
    // end of insertion
        appInsights.trackPageView();
        appInsights.trackTrace("sssssss114");
        appInsights.trackDependency("id", "GET", "https://www.bing.com", null, null, true, null, { prop1: 'abc' }, { meas1: 4.5 })
    </script>
    

    And the dependency data is shown in azure portal -> application insights logs:

    enter image description here