-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increases specificity of localStorage keys to differentiate events by…
… writeKey (#861)
- Loading branch information
1 parent
40745ff
commit 99402e9
Showing
19 changed files
with
355 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-next': patch | ||
--- | ||
|
||
Fixes issue related to how retried events are stored in localStorage to prevent analytics.js from reading events for a different writeKey when that writeKey is used on the same domain as the current analytics.js. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/browser-integration-tests/src/helpers/extract-writekey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function extractWriteKeyFromUrl(url: string): string | undefined { | ||
const matches = url.match( | ||
/https:\/\/cdn.segment.com\/v1\/projects\/(.+)\/settings/ | ||
) | ||
|
||
if (matches) { | ||
return matches[1] | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/browser-integration-tests/src/helpers/get-persisted-items.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export interface PersistedQueueResult { | ||
key: string | ||
name: string | ||
messageIds: string[] | ||
writeKey?: string | ||
} | ||
|
||
export function getPersistedItems(): PersistedQueueResult[] { | ||
const results: PersistedQueueResult[] = [] | ||
|
||
for (let i = 0; i < window.localStorage.length; i++) { | ||
const key = window.localStorage.key(i) | ||
if ( | ||
key && | ||
key.startsWith('persisted-queue:v1:') && | ||
key.endsWith(':items') | ||
) { | ||
const value = window.localStorage.getItem(key) | ||
const messageIds = value | ||
? JSON.parse(value).map((i: any) => i.event.messageId) | ||
: [] | ||
|
||
// Key looks like either: | ||
// new keys - persisted-queue:v1:writeKey:dest-Segment.io:items | ||
// old keys - persisted-queue:v1:dest-Segment.io:items | ||
const components = key.split(':') | ||
let writeKey: string | undefined | ||
let name: string | ||
|
||
if (components.length === 5) { | ||
;[, , writeKey, name] = components | ||
} else if (components.length === 4) { | ||
;[, , name] = components | ||
} else { | ||
throw new Error('Unrecognized persisted queue key.') | ||
} | ||
results.push({ key, messageIds, name, writeKey }) | ||
} | ||
} | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.