Skip to content

Capture page context as early as possible if initialPageview = true#1009

Merged
silesky merged 1 commit intomasterfrom
fix-initial-pageview
Jan 3, 2024
Merged

Capture page context as early as possible if initialPageview = true#1009
silesky merged 1 commit intomasterfrom
fix-initial-pageview

Conversation

@silesky
Copy link
Copy Markdown
Contributor

@silesky silesky commented Dec 1, 2023

  • Analytics: if initialPageView is true, we want to capture page context before destinations are registered.
  • Consent: If initialPageView is true, we want to capture page context before .load() is called (taking advantage of the fact that page context is buffered when you do analytics.page())

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Dec 1, 2023

🦋 Changeset detected

Latest commit: 58310d6

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

This PR includes changesets to release 3 packages
Name Type
@segment/analytics-next Patch
@segment/analytics-consent-tools Minor
@segment/analytics-consent-wrapper-onetrust Minor

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 changed the title fix initial page view if initialPageView is true, capture page context earlier Dec 1, 2023
@silesky silesky force-pushed the fix-initial-pageview branch from 10e2fb9 to cd3d1ca Compare December 6, 2023 21:18
@silesky silesky marked this pull request as ready for review December 6, 2023 21:25
@silesky silesky changed the title if initialPageView is true, capture page context earlier Capture page context as early as possible if initialPageView is true Dec 6, 2023
@silesky silesky changed the title Capture page context as early as possible if initialPageView is true Capture page context as early as possible if initialPageview setting is true Dec 6, 2023
@silesky silesky changed the title Capture page context as early as possible if initialPageview setting is true Capture page context as early as possible if initialPageview = true Dec 6, 2023
@silesky silesky force-pushed the fix-initial-pageview branch 2 times, most recently from b4010c1 to 0649fdf Compare December 6, 2023 23:32
Comment on lines 13 to 17
export interface InitOptions {
updateCDNSettings?(cdnSettings: CDNSettings): CDNSettings
disable?: boolean | ((cdnSettings: CDNSettings) => boolean)
initialPageview?: boolean
}
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.

why is this object completely detached from the interface of the options of the analytics client? shouldn't they be the same?

Copy link
Copy Markdown
Contributor Author

@silesky silesky Dec 8, 2023

Choose a reason for hiding this comment

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

Here's my original reasoning:

  • Owning all our own interface forces us to be explicit about what features of the analytics client we are actually depending on, which helps with mocking / clarity (obviously you could use pick to grab fields -- but like that breaks down when you have say functions with callbacks that are typed. For example, we don't need all of the cdnSettings interface-- just a piece).
  • We have both e2e and typedef tests to ensure that the real analytics types do not get out of sync with those defined here (that's why analytics-next is a dev dependency).
  • avoiding a hard dependency on analytics-next means users who use the snippet but still build their application with npm don't get the large dependency tree. Also simplifies releasing via changeset (ie, ajs releases won't trigger a consent release by default), possible internal library compilation errors for those with skipLib check false etc. Just simplifies things generally 😄

Comment thread packages/consent/consent-tools/src/domain/create-wrapper.ts Outdated
Comment thread packages/browser/src/browser/index.ts Outdated
@silesky silesky force-pushed the fix-initial-pageview branch from e31b042 to 58310d6 Compare December 14, 2023 21:40
@silesky silesky requested a review from chrisradek December 14, 2023 21:46

if (options.initialPageview) {
// capture the page context early, so it's always up-to-date
preInitBuffer.push(new PreInitMethodCall('page', []))
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.

This doesn't store all the page context like the snippet does...does it? My understanding is that this would queue a page call that will be invoked after some async work has been completed (which gives the address of the page time to change)

Copy link
Copy Markdown
Contributor Author

@silesky silesky Jan 3, 2024

Choose a reason for hiding this comment

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

Yes, it stores the page context at insertion!

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.

Ah right, forgot preInitBuffer isn't a regular array.

@silesky silesky merged commit f476038 into master Jan 3, 2024
@silesky silesky deleted the fix-initial-pageview branch January 3, 2024 23:33
@github-actions github-actions bot mentioned this pull request Jan 3, 2024
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