feat(analytics): add configureAutoTrack support for Kinesis and Kinesis Firehose#14854
feat(analytics): add configureAutoTrack support for Kinesis and Kinesis Firehose#14854soberm wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 4bda657 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
| }; | ||
|
|
||
| // Initialize or update this provider's trackers | ||
| updateProviderTrackers(input, emitTrackingEvent, configuredTrackers); |
There was a problem hiding this comment.
pageView auto-track on two providers silently drops half the page views (shared PageViewTracker global state).
Before this PR, auto-track existed only for Pinpoint, so at most one PageViewTracker could ever exist. This PR makes concurrent instances reachable — e.g. a consumer mid-migration enabling pageView on both Pinpoint and Kinesis. Each provider has its own configuredTrackers singleton, so two independent PageViewTracker objects are created, but the class holds process-global state that isn't namespaced:
-
Shared
sessionStoragekey clobbers events.PageViewTrackeruses a module constantPREV_URL_STORAGE_KEY = 'aws-amplify-analytics-prevUrl'. On each navigation both instances'handleLocationChangefire; the first records and writes the key tocurrentUrl, so the second'surlHasChanged()returns false and it skips recording. Each page view lands in only one provider, chosen by listener order — the consumer expecting page views in both silently loses half. -
History-proxy stacking breaks teardown.
setupSPATrackingcapturesthis.originalPushState = window.history.pushStateand installs a revocable proxy. The second instance captures the first instance's proxy as its "original"; oncleanup()(disable one provider) the restore/revoke order can leave a revoked proxy installed →TypeErroron the nextpushState.
SessionTracker/EventTracker are fine — they fire to distinct destinations, which is the intended "send to both" behavior. This is specific to PageViewTracker's shared sessionStorage key + window.history patching. The per-provider unit tests can't catch it (they mock the trackers and run one provider in isolation).
Not necessarily a merge-blocker, but worth a conscious decision rather than an accident. Two options:
- Deep fix: namespace
PREV_URL_STORAGE_KEYper provider/recorder and make the history patching reference-counted / instance-safe (inPageViewTracker). - Cheap + honest: if multi-provider pageView isn't a supported scenario, document in the
configureAutoTrackJSDoc that pageView auto-track is single-provider.
(Same concern applies to the Firehose configureAutoTrack.ts.)
There was a problem hiding this comment.
Thanks for the careful review. Both issues were valid and are now fixed in commit 92d07a3ca.
1. Shared sessionStorage "previous URL" key. The key is now namespaced per provider (aws-amplify-analytics-prevUrl-<namespace>, e.g. ...-kinesis and ...-kinesis-firehose), so each provider keeps its own previous URL and every provider records the page view instead of the first one clobbering a shared key. Pinpoint keeps the original un namespaced key, so its behavior is unchanged.
2. Stacked per-instance history proxies. Replaced the per-instance revocable proxies with a single reference counted global patch of pushState/replaceState that broadcasts a navigation event, which each tracker subscribes to independently. Teardown decrements the ref count and restores the native pushState/replaceState once the last tracker detaches, so it never leaves a revoked proxy installed and providers can be enabled or disabled in any order.
I also added a regression test at covering concurrent trackers, out of order teardown, and popstate. Beyond the unit tests, I verified this end to end in a real browser against real Kinesis and Firehose: a single SPA navigation is recorded by both providers (confirmed in the delivered data), and disabling providers in a non-LIFO order no longer throws the revoked proxy error.
Description of changes
Adds
configureAutoTrack(auto-tracking) support to the Kinesis and Kinesis Firehose Analytics providers. Auto-tracking (session / pageView / event trackers) previously existed only for Pinpoint; this brings the same capability to the two stream providers.This change is purely additive — Pinpoint behavior is unchanged.
What it adds
configureAutoTrack(input)for both providers, mirroring the existing Pinpoint implementation (module-levelconfiguredTrackerssingleton,updateProviderTrackers+validateTrackerConfiguration, session/pageView/event tracker types).record()API with the data shape{ name: eventName, attributes }(mirrorsPinpointAnalyticsEvent).streamName(andpartitionKeyfor Kinesis) are supplied in eachconfigureAutoTrackcall'soptions, so different tracker types can target different streams. Reconfiguring a tracker updates its stream coordinates.KinesisConfigureAutoTrackInput/KinesisFirehoseConfigureAutoTrackInputand theirSession/PageView/Eventtracking-option types, exported from each provider's public entry point.Kinesis Firehose difference: Firehose has no
partitionKey, so its options only carrystreamNameand itsrecord()call omitspartitionKey.Shared-infra decouple
utils/trackerHelpers.tsnow typesupdateProviderTrackersagainst the genericAnalyticsConfigureAutoTrackInput(from../types/inputs) instead of the Pinpoint-specificConfigureAutoTrackInput. Pinpoint's type is a structural alias of the generic one, so Pinpoint continues to compile and behave identically.Runtime validation (options stay optional in the type; validated at runtime)
AnalyticsValidationErrorCode.NoStreamNamewhenenable: trueandstreamNameis missing.AnalyticsValidationErrorCode.NoPartitionKeywhenenable: trueandpartitionKeyis missing.enable: false.React Native is unchanged — both providers inherit the existing shared validator that restricts auto-tracking to
sessionon native.Issue #, if available
N/A
Description of how you validated changes
__tests__/providers/kinesis/apis/configureAutoTrack.test.ts__tests__/providers/kinesis-firehose/apis/configureAutoTrack.test.ts__tests__/utils/trackerHelpers.test.ts(provesupdateProviderTrackersaccepts a generic, non-Pinpoint input and forwards options; provider-specific fields are ignored by the shared trackers)validateTrackerConfigurationinvoked;NoStreamName/NoPartitionKeythrown when enabling without the required fields; no throw when disabling; session/pageView/event trackers fire and callrecord()with the correct{ streamName[, partitionKey], data: { name, attributes } }; reconfigure updates stream coordinates; disable cleans trackers up.aws-amplifyexports test: passes — 14 tests.exports.test.tsupdated so the Kinesis and Kinesis Firehose expected-export arrays includeconfigureAutoTrack.tsc --noEmit -p tsconfig.build.jsonis clean (exit 0), no new errors and no Pinpoint regressions.Checklist
yarn testpassesChecklist for repo maintainers
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.