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

Support loading analytics into a custom global variable when using snippet version 5.2.1 or later #1032

Merged
merged 4 commits into from
Feb 1, 2024

Conversation

zikaari
Copy link
Contributor

@zikaari zikaari commented Jan 26, 2024

Fixes #1014

Please refer to the original issue for more information

Docs for this feature - segmentio/segment-docs#5949

  • I've included a changeset (psst. run yarn changeset. Read about changesets here).

Copy link

changeset-bot bot commented Jan 26, 2024

🦋 Changeset detected

Latest commit: d6caafa

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 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

@@ -0,0 +1,97 @@
<!DOCTYPE html>
Copy link
Contributor

@silesky silesky Jan 26, 2024

Choose a reason for hiding this comment

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

Following the pattern of the other integration tests -- should we move this file to standalone-playground?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is as per how @chrisradek advised me to setup an integration test

t.src =
'https://cdn.segment.com/analytics.js/v1/' +
key +
'/analytics.min.js'
Copy link
Contributor

@silesky silesky Jan 26, 2024

Choose a reason for hiding this comment

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

Is this testing against the bundle that lives in the monorepo? This is loading the cdn bundle, which doesn't have the updated code AFAIK. Kind of wondering how the tests are passing.

(It's be even cleaner IMO to use the segment snippet npm package with new option).

See the other browser integration test's html for example FYI of how they are symlinked to this bundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, after it's been built

Copy link
Contributor

@silesky silesky Jan 30, 2024

Choose a reason for hiding this comment

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

@zikaari I am still a bit confused -- to rephrase:

If this is loading analytics from the CDN, wouldn't this be currently testing against the version of analytics that doesn't have the changes reflected in this PR (i.e if (globalAnalyticsKey))?

What am I missing? I would expect that the link to look more like https://github.com/segmentio/analytics-next/blob/master/playgrounds/standalone-playground/pages/index-local.html#L79

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This does the redirection -

await context.route(
'https://cdn.segment.com/analytics.js/v1/*/analytics.min.js',
(route, request) => {
if (request.method().toLowerCase() !== 'get') {
return route.continue()
}
return route.fulfill({
path: joinPath(ajsBasePath, 'standalone.js'),
status: 200,
headers: {
'Content-Type': 'text/javascript',
},
})
}
)

Copy link
Contributor

@silesky silesky Jan 30, 2024

Choose a reason for hiding this comment

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

ohhhh... I missed that detail! That's excellent!

analytics.SNIPPET_VERSION = '5.2.0'

// will be used for asserting in the corresponding test
analytics.track('track from snippet')
Copy link
Contributor

@silesky silesky Jan 26, 2024

Choose a reason for hiding this comment

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

a bit confused by this part? Can we make this snippet vanilla via analytics.page, and use page.evaluate to run other code as usual?

Would be even better if this snippet was generated from the npm package, and we can even use route.fulfill to link to the current umd.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

analytics here is a reference to window['segment_analytics']

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with what Seth is saying - this page should just be as vanilla a snippet as can be - we can make the actual track call in the test file.

Also, does the new snippet always set window.analytics even if you're using a custom global key? I think that would cause a problem for users reserving window.analytics for something else. Wouldn't tests using this continue to pass without your a.js changes because window.analytics still exists with this snippet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this page should just be as vanilla a snippet as can be - we can make the actual track call in the test file.

During our meeting we talked about testing if buffered calls (initiated in the snippet, before the main bundle loads) are taken care of safely - making those calls in the test file won't assert what we're trying to assert

does the new snippet always set window.analytics even if you're using a custom global key

No, it does not. Where did you notice a read or write to window.analytics?

Copy link
Contributor

@chrisradek chrisradek Jan 29, 2024

Choose a reason for hiding this comment

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

No, it does not. Where did you notice a read or write to window.analytics?

On the line that's highlighted - analytics.track('track from snippet')

Ah, I may have misread this - apologies and disregard this part then!

making those calls in the test file won't assert what we're trying to assert

Couldn't we make a track call, call custom_analytics.load('writekey', { globalAnalyticsKey: 'custom_analytics' }), and verify we see a network request for the track call we generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you guys are right, its not that we are testing if calls in snippet get dealt with - its the calls before load, and your proposal will cover that. Let me change that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - fc0be16

@silesky
Copy link
Contributor

silesky commented Jan 26, 2024

@chrisradek should we get it out of the way and link to our segment doc update PR here, so it doesn't get lost in the fray?

@silesky silesky closed this Jan 26, 2024
@silesky silesky reopened this Jan 26, 2024
)?.dataset.globalSegmentAnalyticsKey

if (globalAnalyticsKey) {
setGlobalAnalyticsKey(globalAnalyticsKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

This was a good idea to live here, so it doesn't get included in the npm bundle.

Copy link
Contributor

@chrisradek chrisradek left a comment

Choose a reason for hiding this comment

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

I like using the browser integration tests since it means we can test that the actual track calls work on the new global - I had some questions I posted in the PR - is there a docs PR for this change as well? I think we'll have to be very clear on how the snippet has to be modified to work with the globalAnalyticsKey configuration - based on the changelog I wouldn't expect that I'd need a new snippet to make this work.

@zikaari
Copy link
Contributor Author

zikaari commented Jan 29, 2024

My docs PR was 1:1 replica of Seth's PR. Where do I explain the usage of this feature?

// Load analytics.js
await page.goto('/standalone-custom-key.html')
await page.evaluate(() =>
(window as any).segment_analytics.load('fake-key')
Copy link
Contributor

@chrisradek chrisradek Jan 29, 2024

Choose a reason for hiding this comment

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

Ah, so if a custom key is used, we don't actually have to set globalAnalyticsKey? That's nice! (I'm also kind of wondering now what use we get out of the globalAnalyticsKey...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it serves no purpose for snippet users. Unless it serves a utility for NPM users, it is useless.

@chrisradek
Copy link
Contributor

My docs PR was 1:1 replica of segmentio/segment-docs#5754. Where do I explain the usage of this feature?

Do we currently explain anywhere in the docs that you can change the variable used to install analytics? I originally thought you also had to use the globalAnalyticsKey with this feature - but looking at your test looks like that's probably not the case. So might make sense to add another section to Segment Docs on using something other than window.analytics for snippet users.

'@internal/browser-integration-tests': patch
---

Fix globalAnalyticsKey feature
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Fix globalAnalyticsKey feature
Support loading analytics into a custom global variable when using snippet version [X.X.X]+

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - 631923d

@zikaari
Copy link
Contributor Author

zikaari commented Jan 29, 2024

Will add a section to the javascript, what do you think should the section be titled? "Installing Analytics under custom namespace"?

@zikaari zikaari changed the title Fix globalAnalyticsKey not working Support loading analytics into a custom global variable when using snippet version 5.2.1 or later Jan 29, 2024
@chrisradek
Copy link
Contributor

"Installing Analytics under custom namespace"

I think something like that is fine. We can always update it as well, just want to make sure we are documenting our changes - we've not always been great about that historically.

@zikaari
Copy link
Contributor Author

zikaari commented Jan 30, 2024

Docs are here - segmentio/segment-docs#5949

@@ -0,0 +1,6 @@
---
'@segment/analytics-next': patch
'@internal/browser-integration-tests': patch
Copy link
Contributor

@silesky silesky Jan 30, 2024

Choose a reason for hiding this comment

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

This should not really be bumped. It doesn't matter, but we should leave this off the changelog.

@@ -0,0 +1,6 @@
---
'@segment/analytics-next': patch
Copy link
Contributor

@silesky silesky Jan 30, 2024

Choose a reason for hiding this comment

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

I would change this to minor, as this is a new feature IMO (even though it does kind of have a fix element)

@zikaari zikaari merged commit 5c1511f into master Feb 1, 2024
4 checks passed
@zikaari zikaari deleted the global_key_fix branch February 1, 2024 16:51
@github-actions github-actions bot mentioned this pull request Feb 1, 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.

How to use globalAnalyticsKey in the browser, using Vanilla JS?
3 participants