Skip to content

page() does not capture correct path if site uses canonical link tag #2

Description

@andycmaj

my site uses a link rel="canonical" tag to support various crawlers/social share/etc. I do this with react-helmet.

calling analytics.page() on onRouteUpdated doesn't work in this case, because this fires before the react component that renders the meta tag is re-rendered.

so at the time when analytics.js tries to figure out what hte current page path is, it looks at the canonical tag and sees <link data-react-helmet="true" rel="canonical" href="https://todayilearned.io/">.

so page() path is set to /.

ScreenCapture at Thu Mar 14 23:56:00 PDT 2019

ScreenCapture at Thu Mar 14 23:56:38 PDT 2019

the fix could be to use the location.pathname passed to the onRouteUpdate hook, as it's the correct route at that point:

ScreenCapture at Thu Mar 14 23:57:15 PDT 2019

segment.js does allow passing in the path manually: https://segment.com/docs/sources/website/analytics.js/#default-properties

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions