Skip to content

Fix wrong utm parsing by making context.page.search the source of truth for query params.#839

Merged
silesky merged 4 commits intomasterfrom
simple-fix-utm-late-context-parsing
Apr 17, 2023
Merged

Fix wrong utm parsing by making context.page.search the source of truth for query params.#839
silesky merged 4 commits intomasterfrom
simple-fix-utm-late-context-parsing

Conversation

@silesky
Copy link
Copy Markdown
Contributor

@silesky silesky commented Apr 16, 2023

Fixes a utm-parameter parsing bug where overridden page.search properties would not be reflected in the context.campaign object

analytics.page(undefined, undefined, {search: "?utm_source=123&utm_content=content" )
analytics.track("foo", {url: "....", search: "?utm_source=123&utm_content=content" )

// should result in a context.campaign of:
{ source: 123, content: 'content'}

This fix is part 2 --
See: #838

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 16, 2023

🦋 Changeset detected

Latest commit: e7e3d75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@segment/analytics-next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@silesky silesky marked this pull request as draft April 16, 2023 07:30
@silesky silesky changed the title Simple fix utm late context parsing Fix utm late page parsing Apr 16, 2023
@silesky silesky force-pushed the simple-fix-utm-late-context-parsing branch from 29e908e to cc06181 Compare April 17, 2023 20:02
@silesky silesky requested review from chrisradek, danieljackins and pooyaj and removed request for chrisradek April 17, 2023 20:08
json.context = json.context ?? json.options ?? {}
const ctx = json.context

// This guard should not be neccessary -- why would context not exist here? Ditto ^ --
Copy link
Copy Markdown
Contributor Author

@silesky silesky Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pooyaj might have some insight here?

The presence of this useless guard against missing context makes me think that this logic was written before Page Enrichment, and this is tech debt. This points to a broader refactor : It would be simpler / make sense if the page enrichment plugin was was also responsible for this logic:

  if (query && !ctx.campaign) {
    ctx.campaign = utm(query)
  }

If the current bugfix is safe, then moving the logic up to page enrichment (a "before" plugin) altogether should be equally safe / rational. Let me know if I'm missing something!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is mostly legacy behavior from AJS classic where the UTM params were injected in the Segment integration rather than in AJS core by accessing window.location.search directly. I think this change is great. But moving the UTM logic to the page enrichment plugin has some consequences (i.e., UTM params will be sent as a part of the context to all destinations ). I'm not sure if that would be ok or not tho.

Copy link
Copy Markdown
Contributor Author

@silesky silesky Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I guess we've decided that, while we pass the query string, we don't want to pass ctx.campaign to all destinations.

  1. Do you have any insight into this line (i.e. can we just delete this?):
json.context = json.context ?? json.options ?? {}
  1. .context should always be defined (see page enrichment plugin). if it gets deleted by a bad plugin, that should be a runtime error.
  2. even if .context somehow wasn't defined (i.e. deleted by some custom plugin), json.options as a fallback? why would we ever want to set event.context to:
  integrations?:
  timestamp?:
  context?: // (event.context.context?) lol
  anonymousId?:
  userId?: 
  traits?: // this is the only one that makes sense

@silesky silesky marked this pull request as ready for review April 17, 2023 20:20
@silesky silesky changed the title Fix utm late page parsing Fix wrong utm parsing by making context.page.search the source of truth for query params. Apr 17, 2023
json.context = json.context ?? json.options ?? {}
const ctx = json.context

// This guard should not be neccessary -- why would context not exist here? Ditto ^ --
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is mostly legacy behavior from AJS classic where the UTM params were injected in the Segment integration rather than in AJS core by accessing window.location.search directly. I think this change is great. But moving the UTM logic to the page enrichment plugin has some consequences (i.e., UTM params will be sent as a part of the context to all destinations ). I'm not sure if that would be ok or not tho.

Copy link
Copy Markdown
Contributor

@danieljackins danieljackins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me, might be worth testing it out on one of the affected customer pages as an override

@silesky
Copy link
Copy Markdown
Contributor Author

silesky commented Apr 17, 2023

Changes look good to me, might be worth testing it out on one of the affected customer pages as an override

Yep, I've tested it. FTR, this is not the end of the fix -- this just enables a bandaid where upset customers can fix staleness by using manually passing page props (e.g. {url: "foo"}) in their page calls.

The underlying problem is that our buffered queue has never captured page information at call time -- assuming, wrongly that when analytics loads, the info will be the same.

@silesky silesky merged commit fdc004b into master Apr 17, 2023
@silesky silesky deleted the simple-fix-utm-late-context-parsing branch April 17, 2023 21:41
@github-actions github-actions bot mentioned this pull request Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants