@funnelsgrove/analytics 0.1.51 → 0.1.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ Read this file before editing analytics code. Deeper runtime SDK docs live in
|
|
|
34
34
|
- `bootstrapPublicAnalyticsUser`: public SDK identity bootstrap helper.
|
|
35
35
|
- `sdk-user-id.storage`: shared browser user-id storage contract.
|
|
36
36
|
- `buildStripePurchaseMetadata` and purchase attribute helpers: safe metadata shaping for payment handoff.
|
|
37
|
+
- `trackStripePaymentCheckoutReturn`: parses and deduplicates Stripe browser-return diagnostics without claiming a purchase conversion.
|
|
37
38
|
- `google-tag.service` and `meta-pixel.service`: partner event adapters.
|
|
38
39
|
- `experiment-result-metrics.service`: shared experiment result metric helpers.
|
|
39
40
|
|
|
@@ -43,6 +44,7 @@ Read this file before editing analytics code. Deeper runtime SDK docs live in
|
|
|
43
44
|
- Keep provider adapters dumb: they map canonical events, not funnel business flow.
|
|
44
45
|
- Do not import payment UI or runtime routing from this package.
|
|
45
46
|
- Sensitive values must be filtered before metadata reaches Stripe or partner tools.
|
|
47
|
+
- A Stripe browser return is diagnostic state only. Use `trackStripePaymentCheckoutReturn`; never turn query parameters into `checkout_completed` or trusted `purchase_completed`.
|
|
46
48
|
|
|
47
49
|
## Google Tag Configuration
|
|
48
50
|
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export * from './services/google-tag.service.js';
|
|
|
5
5
|
export * from './services/meta-pixel.service.js';
|
|
6
6
|
export * from './services/purchase-attributes.service.js';
|
|
7
7
|
export * from './services/public-analytics-sdk.service.js';
|
|
8
|
+
export * from './services/payment-checkout-return.service.js';
|
|
8
9
|
export * from './services/experiment-result-metrics.service.js';
|
package/dist/index.js
CHANGED
|
@@ -8,4 +8,5 @@ export * from './services/meta-pixel.service.js';
|
|
|
8
8
|
// Purchase and event transport.
|
|
9
9
|
export * from './services/purchase-attributes.service.js';
|
|
10
10
|
export * from './services/public-analytics-sdk.service.js';
|
|
11
|
+
export * from './services/payment-checkout-return.service.js';
|
|
11
12
|
export * from './services/experiment-result-metrics.service.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type PublicAnalyticsSdkService, type PublicSdkStepConversionCallbackInput } from './public-analytics-sdk.service.js';
|
|
2
|
+
type PaymentCheckoutReturnAnalytics = Pick<PublicAnalyticsSdkService, 'flush' | 'trackPaymentCheckoutReturned' | 'trackPaymentCheckoutSucceeded'>;
|
|
3
|
+
type PaymentCheckoutReturnStorage = Pick<Storage, 'getItem' | 'setItem'>;
|
|
4
|
+
export type StripePaymentCheckoutReturn = {
|
|
5
|
+
paymentIntentId: string;
|
|
6
|
+
redirectStatus: string;
|
|
7
|
+
};
|
|
8
|
+
export type TrackStripePaymentCheckoutReturnInput = Omit<PublicSdkStepConversionCallbackInput, 'eventId' | 'metadata' | 'occurredAt'> & {
|
|
9
|
+
analytics?: PaymentCheckoutReturnAnalytics;
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
occurredAt?: string;
|
|
12
|
+
search?: string;
|
|
13
|
+
storage?: PaymentCheckoutReturnStorage | null;
|
|
14
|
+
};
|
|
15
|
+
export type TrackStripePaymentCheckoutReturnResult = {
|
|
16
|
+
eventId?: string;
|
|
17
|
+
outcome: 'analytics_rejected' | 'already_tracked' | 'not_present' | 'returned' | 'succeeded';
|
|
18
|
+
paymentIntentId?: string;
|
|
19
|
+
redirectStatus?: string;
|
|
20
|
+
};
|
|
21
|
+
export declare const parseStripePaymentCheckoutReturn: (search: string) => StripePaymentCheckoutReturn | null;
|
|
22
|
+
export declare const trackStripePaymentCheckoutReturn: (input: TrackStripePaymentCheckoutReturnInput) => Promise<TrackStripePaymentCheckoutReturnResult>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { publicAnalyticsSdk, } from './public-analytics-sdk.service.js';
|
|
2
|
+
const STRIPE_PAYMENT_INTENT_QUERY_PARAM = 'payment_intent';
|
|
3
|
+
const STRIPE_REDIRECT_STATUS_QUERY_PARAM = 'redirect_status';
|
|
4
|
+
const RETURN_EVENT_ID_PREFIX = 'payment_checkout_return';
|
|
5
|
+
const RETURN_STORAGE_KEY_PREFIX = 'funnelsgrove:analytics:payment-checkout-return';
|
|
6
|
+
const STRIPE_PAYMENT_INTENT_ID = /^pi_[A-Za-z0-9_]+$/;
|
|
7
|
+
const STRIPE_REDIRECT_STATUSES = new Set([
|
|
8
|
+
'canceled',
|
|
9
|
+
'processing',
|
|
10
|
+
'requires_action',
|
|
11
|
+
'requires_capture',
|
|
12
|
+
'requires_confirmation',
|
|
13
|
+
'requires_payment_method',
|
|
14
|
+
'succeeded',
|
|
15
|
+
]);
|
|
16
|
+
const asValidQueryValue = (value) => {
|
|
17
|
+
const normalized = (value === null || value === void 0 ? void 0 : value.trim()) || '';
|
|
18
|
+
if (!normalized
|
|
19
|
+
|| normalized === 'null'
|
|
20
|
+
|| normalized === 'undefined'
|
|
21
|
+
|| /^(\{.*\}|\[.*\])$/.test(normalized)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return normalized;
|
|
25
|
+
};
|
|
26
|
+
const readBrowserSearch = () => {
|
|
27
|
+
return typeof window === 'undefined' ? '' : window.location.search;
|
|
28
|
+
};
|
|
29
|
+
const readBrowserSessionStorage = () => {
|
|
30
|
+
if (typeof window === 'undefined') {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return window.sessionStorage;
|
|
35
|
+
}
|
|
36
|
+
catch (_a) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const hasStoredReturnEvent = (storage, storageKey) => {
|
|
41
|
+
if (!storage) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return storage.getItem(storageKey) === '1';
|
|
46
|
+
}
|
|
47
|
+
catch (_a) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const storeReturnEvent = (storage, storageKey) => {
|
|
52
|
+
if (!storage) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
storage.setItem(storageKey, '1');
|
|
57
|
+
}
|
|
58
|
+
catch (_a) {
|
|
59
|
+
// Analytics delivery must not break a completion screen when storage is unavailable.
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export const parseStripePaymentCheckoutReturn = (search) => {
|
|
63
|
+
const searchParams = new URLSearchParams(search);
|
|
64
|
+
const paymentIntentId = asValidQueryValue(searchParams.get(STRIPE_PAYMENT_INTENT_QUERY_PARAM));
|
|
65
|
+
const redirectStatus = asValidQueryValue(searchParams.get(STRIPE_REDIRECT_STATUS_QUERY_PARAM));
|
|
66
|
+
if (!paymentIntentId
|
|
67
|
+
|| !STRIPE_PAYMENT_INTENT_ID.test(paymentIntentId)
|
|
68
|
+
|| !redirectStatus
|
|
69
|
+
|| !STRIPE_REDIRECT_STATUSES.has(redirectStatus)) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
paymentIntentId,
|
|
74
|
+
redirectStatus,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
export const trackStripePaymentCheckoutReturn = async (input) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
const checkoutReturn = parseStripePaymentCheckoutReturn((_a = input.search) !== null && _a !== void 0 ? _a : readBrowserSearch());
|
|
80
|
+
if (!checkoutReturn) {
|
|
81
|
+
return { outcome: 'not_present' };
|
|
82
|
+
}
|
|
83
|
+
const eventId = [
|
|
84
|
+
RETURN_EVENT_ID_PREFIX,
|
|
85
|
+
checkoutReturn.paymentIntentId,
|
|
86
|
+
checkoutReturn.redirectStatus,
|
|
87
|
+
].join(':');
|
|
88
|
+
const storageKey = `${RETURN_STORAGE_KEY_PREFIX}:${eventId}`;
|
|
89
|
+
const storage = input.storage === undefined ? readBrowserSessionStorage() : input.storage;
|
|
90
|
+
if (hasStoredReturnEvent(storage, storageKey)) {
|
|
91
|
+
return Object.assign(Object.assign({}, checkoutReturn), { eventId, outcome: 'already_tracked' });
|
|
92
|
+
}
|
|
93
|
+
const analytics = (_b = input.analytics) !== null && _b !== void 0 ? _b : publicAnalyticsSdk;
|
|
94
|
+
const track = checkoutReturn.redirectStatus === 'succeeded'
|
|
95
|
+
? analytics.trackPaymentCheckoutSucceeded.bind(analytics)
|
|
96
|
+
: analytics.trackPaymentCheckoutReturned.bind(analytics);
|
|
97
|
+
let acceptedEventId = null;
|
|
98
|
+
try {
|
|
99
|
+
acceptedEventId = track({
|
|
100
|
+
userId: input.userId,
|
|
101
|
+
environment: input.environment,
|
|
102
|
+
eventId,
|
|
103
|
+
occurredAt: input.occurredAt,
|
|
104
|
+
stepId: input.stepId,
|
|
105
|
+
stepName: input.stepName,
|
|
106
|
+
stepType: input.stepType,
|
|
107
|
+
stepContractVersion: input.stepContractVersion,
|
|
108
|
+
metadata: Object.assign(Object.assign({}, input.metadata), { paymentIntentId: checkoutReturn.paymentIntentId, redirectStatus: checkoutReturn.redirectStatus, source: 'stripe_return' }),
|
|
109
|
+
featureFlags: input.featureFlags,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (_c) {
|
|
113
|
+
// Browser-return diagnostics must never break the completion screen.
|
|
114
|
+
}
|
|
115
|
+
if (!acceptedEventId) {
|
|
116
|
+
return Object.assign(Object.assign({}, checkoutReturn), { eventId, outcome: 'analytics_rejected' });
|
|
117
|
+
}
|
|
118
|
+
storeReturnEvent(storage, storageKey);
|
|
119
|
+
try {
|
|
120
|
+
await analytics.flush();
|
|
121
|
+
}
|
|
122
|
+
catch (_d) {
|
|
123
|
+
// The SDK retains its own delivery guarantees; the completion screen should continue.
|
|
124
|
+
}
|
|
125
|
+
return Object.assign(Object.assign({}, checkoutReturn), { eventId, outcome: checkoutReturn.redirectStatus === 'succeeded' ? 'succeeded' : 'returned' });
|
|
126
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funnelsgrove/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.52",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"test:run": "vitest run --passWithNoTests"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@funnelsgrove/runtime": "0.7.
|
|
39
|
+
"@funnelsgrove/runtime": "0.7.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^20",
|