Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ai_user and ai_session cookies not set #1202

Closed
acorkery opened this issue Feb 26, 2020 · 4 comments
Closed

[BUG] ai_user and ai_session cookies not set #1202

acorkery opened this issue Feb 26, 2020 · 4 comments
Assignees
Labels

Comments

@acorkery
Copy link

Description/Screenshot
When viewing the User and Session information in the Application Insights dashboards in the portal, the counts of users/sessions are not accurate - it appears to be counting every request as a new unique user and session.

I believe this is due to the fact that ai_user and ai_session cookies are not being set in the user's browsers. I've checked local security settings and cookies are allowed for the domain.

Steps to Reproduce

  1. Create a new web page
  2. Add the default AI JS snippet to the head, setting the instrumentation key
  3. Browse the site in Chrome 80 on Windows 10
  4. Open Chrome Dev tools and check if ai_user and ai_session cookies are set
  5. Check Application Insights to see if it's accurately reporting the number of users and requests per browsing session.
  • OS/Browser: Windows 10/Chrome 80.0.3987.122 (64-bit)
  • SDK Version [e.g. 22]: 2.4.4
  • How you initialized the SDK: Default JS snippet added to head of page, no customizations other than the instrumentation key.
    <script type="text/javascript">
        var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(n){var o={config:n,initialize:!0},t=document,e=window,i="script";setTimeout(function(){var e=t.createElement(i);e.src=n.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js",t.getElementsByTagName(i)[0].parentNode.appendChild(e)});try{o.cookie=t.cookie}catch(e){}function a(n){o[n]=function(){var e=arguments;o.queue.push(function(){o[n].apply(o,e)})}}o.queue=[],o.version=2;for(var s=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];s.length;)a("track"+s.pop());var r="Track",c=r+"Page";a("start"+c),a("stop"+c);var u=r+"Event";if(a("start"+u),a("stop"+u),a("addTelemetryInitializer"),a("setAuthenticatedUserContext"),a("clearAuthenticatedUserContext"),a("flush"),o.SeverityLevel={Verbose:0,Information:1,Warning:2,Error:3,Critical:4},!(!0===n.disableExceptionTracking||n.extensionConfig&&n.extensionConfig.ApplicationInsightsAnalytics&&!0===n.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){a("_"+(s="onerror"));var p=e[s];e[s]=function(e,n,t,i,a){var r=p&&p(e,n,t,i,a);return!0!==r&&o["_"+s]({message:e,url:n,lineNumber:t,columnNumber:i,error:a}),r},n.autoExceptionInstrumented=!0}return o}(
        {
            instrumentationKey:"HARDCODED_VALUE_REMOVED"
        }
        );(window[aiName]=aisdk).queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
    </script>

Expected behavior
When viewing the User and Session information in the Application Insights dashboards in the portal, the counts of users/sessions should accurately reflect the number of users browsing the site.

However, it appears to be counting every request as a new user and session. From digging around with dev tools, it appears the ai_user and ai_session cookies are not being set. The AI JS is initializing and tracking correctly, but it appears to be unable to support setting the id cookies.

Additional context
I downloaded the latest SDK and referenced it from my website and debugged through the code. The following piece of code from the SDK library is relevant to this issue

        /*
         * helper method to tell if document.cookie object is available
         */
        Util.canUseCookies = function (logger) {
            if (CoreUtils._canUseCookies === undefined) {
                CoreUtils._canUseCookies = false;
                try {
                    CoreUtils._canUseCookies = Util.document.cookie !== undefined;
                }
                catch (e) {
                    logger.throwInternal(LoggingSeverity.WARNING, _InternalMessageId.CannotAccessCookie, "Cannot access document.cookie - " + Util.getExceptionName(e), { exception: Util.dump(e) });
                }
            }
            return CoreUtils._canUseCookies && Util.document && Util.document.cookie;
        };

When debugging, the variable Util.document.cookie always returns a value of "", meaning that the canUseCookies function always returns false (at least in my environment).

When I remove the last condition && Util.document.cookie, the two cookies - ai_user and ai_session are then set correctly and tracking works as expected.

I am also seeing the same behavior in Microsoft Edge version 44.18362.449.0

@acorkery acorkery changed the title [BUG] ai_user and ai_session cookie not set [BUG] ai_user and ai_session cookies not set Feb 26, 2020
@MSNev MSNev self-assigned this Feb 26, 2020
@MSNev
Copy link
Collaborator

MSNev commented Feb 26, 2020

Fix committed, will close once we release the next build (should be within the next week)

@MSNev
Copy link
Collaborator

MSNev commented Mar 10, 2020

The fix is included in version 2.5.1 which has already been released to NPM and is scheduled to be released to the main CDN channel tomorrow. At which point I'll close this issue.

@MSNev
Copy link
Collaborator

MSNev commented Mar 11, 2020

Version 2.5.2 is now fully deployed to the CDN, closing as this should address this issue.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants