ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

Documentation for addTelemetryInitializer is unclear

Open lukos opened this issue 5 years ago • 13 comments

I am trying to use addTelemetryInitializer as per the documentation but it is unclear because the docs (API-reference.md) refer to:

window.appInsights = appInsights;

// Add telemetry initializer
appInsights.trackPageView();

but the JS code from the portal does not look like that, it looks like this:

var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var etc...
{
     instrumentationKey:"key"
}); window[aiName] = aisdk,aisdk.queue && aisdk.queue.length ===0 && aisdk.trackPageView({});;

I tried splitting the last line and ended up with

}); window[aiName];
// Add my initializer here
aisdk.queue && aisdk.trackPageView({});

But this doesn't seem to work. The initializer is not called and I cannot see the call to track in the network monitor. Is there some more clear documentation about how this works with the new JS snippet?

lukos avatar Aug 20 '19 11:08 lukos

I've arrived here as I have exactly the same problem. All the examples on how to configure initializers refer to an old code snippet, and we can't work out how to get them working with the current one.

SimonCaldwell avatar Oct 04 '19 11:10 SimonCaldwell

Same here. Searched all of github for someone adding an application version to telemetry and there's not a single instance of someone using the new snippet and adding any custom initializers. All my attempts to add a TelemetryInitializer with the current snippet result in the page going untracked (even when the queue empty test is removed). Really odd since this has to be the #1 method for people using AI.

jakenuts avatar Jan 02 '20 15:01 jakenuts

Aha! Here is one that works.. I left in the check for an empty queue as it seems like a safety check to make sure a duplicate snippet doesn't cause the page to track twice.. Turns out the non-minified snippet (here) sets up the addTelemetryInitializer function to automatically queue calls so you can use it right away rather than pushing your own function on the queue.

    var sdkInstance="appInsightsSDK";... (
    {
      instrumentationKey:"<key>"
    }
    );window[aiName]=aisdk; /*removed track here*/
       
    if (aisdk.queue && 0 === aisdk.queue.length) {
        aisdk.addTelemetryInitializer(function (evlp) {
            evlp.tags["ai.application.ver"] = '<your app version>;
        });        

        aisdk.trackPageView({}); 
    }

jakenuts avatar Jan 02 '20 16:01 jakenuts

Bump. I can't believe this has not been updated on docs. Surely a 2 minute question to a dev and 1 minute of update is all that is required?

lukos avatar Jun 02 '20 15:06 lukos

Which code snippet are you using where this is not working? Does this sample not work for you? Are you seeing it documented differently elsewhere?

I've put out a change request for this one, and so I am curious if you are finding any inaccuracies elsewhere.

markwolff avatar Jun 02 '20 16:06 markwolff

@markwolff The JS snippet that we are given to paste in calls TrackPageView() at the end of it so the examples to add an initializer won't work, the page is already tracked. It would be clearer all round if the TrackPageView bit was separated so you could then add any other initialization you need to before trackPageView is called.

lukos avatar Jun 08 '20 14:06 lukos

I ended up using @jakenuts workaround although due to the raft of different docs and versions, I was using an older version of the snippet which didn't support addTelemetryInitializer. I also couldn't find the properties of the envelope documented anywhere so I had to use the debugger to work out where everything is.

For example, if you want to rewrite the operation name, you can do this in your telemetryInitializer:

envelope.baseData.uri = envelope.baseData.uri.replace(/[0-9]{4,15}/gi, '{id}');

lukos avatar Jun 08 '20 16:06 lukos

Adding version 5 of the snippet (to address a couple of minor bugs), and also adding #1403

MSNev avatar Oct 13 '20 02:10 MSNev

This Issue will be closed in 30 days. Please remove the "Stale" label or comment to avoid closure with no action.

github-actions[bot] avatar Aug 09 '21 07:08 github-actions[bot]

How do we reset or update the ai_session cookie every time the user login? I am totally new to this and am not sure where I need to do this? tried
appInsights.context.session.id = guid.toString();

jvrakesh avatar Aug 27 '21 20:08 jvrakesh

You almost have it, this should do the trick

appInsights.context.session.id = guid.toString();
appInsights.context.session.update();

MSNev avatar Aug 27 '21 21:08 MSNev

It com

You almost have it, this should do the trick

appInsights.context.session.id = guid.toString();
appInsights.context.session.update();

It complains about the property update does not exists. am I missing something?

jvrakesh avatar Aug 27 '21 21:08 jvrakesh

Moving this session discussion to a different issue as it's not related to this one #1646

MSNev avatar Aug 27 '21 21:08 MSNev