-
Notifications
You must be signed in to change notification settings - Fork 135
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
Conversation
🦋 Changeset detectedLatest commit: d6caafa The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does the redirection -
analytics-next/packages/browser-integration-tests/src/helpers/standalone-mock.ts
Lines 18 to 33 in d4f6975
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', | |
}, | |
}) | |
} | |
) |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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']
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - fc0be16
@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? |
)?.dataset.globalSegmentAnalyticsKey | ||
|
||
if (globalAnalyticsKey) { | ||
setGlobalAnalyticsKey(globalAnalyticsKey) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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') |
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
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.
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 |
.changeset/kind-crabs-report.md
Outdated
'@internal/browser-integration-tests': patch | ||
--- | ||
|
||
Fix globalAnalyticsKey feature |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix globalAnalyticsKey feature | |
Support loading analytics into a custom global variable when using snippet version [X.X.X]+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - 631923d
Will add a section to the javascript, what do you think should the section be titled? "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. |
Docs are here - segmentio/segment-docs#5949 |
.changeset/kind-crabs-report.md
Outdated
@@ -0,0 +1,6 @@ | |||
--- | |||
'@segment/analytics-next': patch | |||
'@internal/browser-integration-tests': patch |
There was a problem hiding this comment.
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.
.changeset/kind-crabs-report.md
Outdated
@@ -0,0 +1,6 @@ | |||
--- | |||
'@segment/analytics-next': patch |
There was a problem hiding this comment.
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)
Fixes #1014
Please refer to the original issue for more information
Docs for this feature - segmentio/segment-docs#5949
yarn changeset
. Read about changesets here).