@funnelsgrove/analytics 0.29.0 → 0.31.0
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { analyticsUrlSanitizerSource, sanitizeAnalyticsExport, sanitizeAnalyticsUrl } from './analytics-export.js';
|
|
2
|
-
import { FUNNEL_ID, FUNNEL_VERSION_ID, GOOGLE_TAG_CONFIG_IDS, GOOGLE_TAG_ENABLED, GOOGLE_TAG_LEAD_VALUE, GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_MANAGER_ID, GOOGLE_TAG_PURCHASE_SEND_TO, GOOGLE_TAG_SCRIPT_ID, PROJECT_ID, STRIPE_ACTIVE_MODE, } from './runtime-api.config.js';
|
|
2
|
+
import { FUNNEL_ID, FUNNEL_VERSION_ID, GOOGLE_ANALYTICS_ID, GOOGLE_TAG_CONFIG_IDS, GOOGLE_TAG_ENABLED, GOOGLE_TAG_LEAD_VALUE, GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_MANAGER_ID, GOOGLE_TAG_PURCHASE_SEND_TO, GOOGLE_TAG_SCRIPT_ID, PROJECT_ID, STRIPE_ACTIVE_MODE, } from './runtime-api.config.js';
|
|
3
3
|
import { isNonLiveAnalyticsRuntime, nonLiveBrowserLocationSource } from './non-live-runtime.js';
|
|
4
|
+
import { readExperimentExposureProperties } from '@funnelsgrove/runtime';
|
|
4
5
|
import { buildBrowserPurchaseAnalyticsAttributes, buildPurchaseAnalyticsAttributes, currentBrowserPurchaseUrl, } from './purchase-attributes.service.js';
|
|
5
6
|
const buildGoogleAnalyticsScriptSrc = (googleAnalyticsId) => `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(googleAnalyticsId)}`;
|
|
6
7
|
const buildGoogleTagManagerScriptSrc = (googleTagManagerId) => `https://www.googletagmanager.com/gtm.js?id=${encodeURIComponent(googleTagManagerId)}`;
|
|
@@ -62,6 +63,27 @@ const buildCommerceParameters = (event, options = {}) => {
|
|
|
62
63
|
const buildGoogleAdsConversionParameters = (parameters, sendTo) => {
|
|
63
64
|
return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign(Object.assign({}, parameters), { send_to: sendTo })));
|
|
64
65
|
};
|
|
66
|
+
// GA4's automatic events (first_visit, session_start, scroll) carry only what the tag config holds.
|
|
67
|
+
// Inlined into the head bootstrap, so it must stay self-contained.
|
|
68
|
+
const readGoogleTagFunnelParameters = (funnelId, funnelVersionId) => {
|
|
69
|
+
var _a, _b, _c, _d;
|
|
70
|
+
const parameters = {};
|
|
71
|
+
if (funnelId)
|
|
72
|
+
parameters.funnel_id = funnelId;
|
|
73
|
+
if (funnelVersionId)
|
|
74
|
+
parameters.funnel_version_id = funnelVersionId;
|
|
75
|
+
const marker = typeof document === 'undefined'
|
|
76
|
+
? null
|
|
77
|
+
: (_a = document.querySelector) === null || _a === void 0 ? void 0 : _a.call(document, 'meta[name="fg-cross-funnel-assignment"]');
|
|
78
|
+
const experimentId = (_b = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-experiment-id')) === null || _b === void 0 ? void 0 : _b.trim();
|
|
79
|
+
const visitorId = (_c = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-visitor-id')) === null || _c === void 0 ? void 0 : _c.trim();
|
|
80
|
+
const variantKey = (_d = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-variant-key')) === null || _d === void 0 ? void 0 : _d.trim();
|
|
81
|
+
if (experimentId && visitorId && variantKey) {
|
|
82
|
+
parameters.cross_funnel_experiment_id = experimentId;
|
|
83
|
+
parameters.cross_funnel_variant_key = variantKey;
|
|
84
|
+
}
|
|
85
|
+
return parameters;
|
|
86
|
+
};
|
|
65
87
|
export const buildGoogleTagHeadScripts = () => {
|
|
66
88
|
if (!GOOGLE_TAG_ENABLED || (!GOOGLE_TAG_SCRIPT_ID && !GOOGLE_TAG_MANAGER_ID) || STRIPE_ACTIVE_MODE === 'test') {
|
|
67
89
|
return [];
|
|
@@ -82,15 +104,17 @@ export const buildGoogleTagHeadScripts = () => {
|
|
|
82
104
|
`window.__funnelsgroveSanitizeGoogleTagUrl = ${analyticsUrlSanitizerSource};`,
|
|
83
105
|
'window.__funnelsgroveGoogleTagPageLocation = window.__funnelsgroveSanitizeGoogleTagUrl(window.location.href);',
|
|
84
106
|
'window.__funnelsgroveGoogleTagHeadReady = true;',
|
|
107
|
+
`const funnelParameters = (${readGoogleTagFunnelParameters.toString()})(${JSON.stringify(FUNNEL_ID)}, ${JSON.stringify(FUNNEL_VERSION_ID)});`,
|
|
85
108
|
...(GOOGLE_TAG_CONFIG_IDS.length > 0
|
|
86
109
|
? [
|
|
87
110
|
'window.gtag("js", new Date());',
|
|
88
|
-
...GOOGLE_TAG_CONFIG_IDS.map((googleTagConfigId) => `window.gtag("config", ${JSON.stringify(googleTagConfigId)}, { page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false });`),
|
|
111
|
+
...GOOGLE_TAG_CONFIG_IDS.map((googleTagConfigId) => `window.gtag("config", ${JSON.stringify(googleTagConfigId)}, Object.assign({ page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false }${googleTagConfigId === GOOGLE_ANALYTICS_ID ? ', funnelParameters' : ''}));`),
|
|
89
112
|
]
|
|
90
113
|
: []),
|
|
91
114
|
...(GOOGLE_TAG_MANAGER_ID
|
|
92
115
|
? [
|
|
93
116
|
'if (!window.fbq) { window.fbq = function(){ window.fbq.callMethod ? window.fbq.callMethod.apply(window.fbq, arguments) : window.fbq.queue.push(arguments); }; window._fbq = window._fbq || window.fbq; window.fbq.push = window.fbq; window.fbq.loaded = true; window.fbq.version = "2.0"; window.fbq.queue = []; }',
|
|
117
|
+
'window.dataLayer.push(funnelParameters);',
|
|
94
118
|
'window.dataLayer.push({"gtm.start": Date.now(), event: "gtm.js"});',
|
|
95
119
|
]
|
|
96
120
|
: []),
|
|
@@ -104,17 +128,13 @@ export const buildGoogleTagHeadScripts = () => {
|
|
|
104
128
|
},
|
|
105
129
|
];
|
|
106
130
|
};
|
|
131
|
+
// GA4 keeps only the first 100 characters of an event parameter value.
|
|
132
|
+
const GA4_PARAMETER_VALUE_LIMIT = 100;
|
|
107
133
|
// Identifies the funnel without parsing page_location, matching PostHog: the deployment config wins over metadata.
|
|
108
134
|
const buildFunnelContextParameters = (event) => {
|
|
109
135
|
var _a;
|
|
110
136
|
const metadata = (_a = event.metadata) !== null && _a !== void 0 ? _a : {};
|
|
111
|
-
return buildDefinedRecord({
|
|
112
|
-
funnel_id: firstString(FUNNEL_ID, metadata.funnelId, metadata.funnel_id),
|
|
113
|
-
funnel_version_id: firstString(FUNNEL_VERSION_ID, metadata.funnelVersionId, metadata.funnel_version_id),
|
|
114
|
-
project_id: firstString(PROJECT_ID, metadata.projectId, metadata.project_id),
|
|
115
|
-
step_type: firstString(metadata.stepType, metadata.step_type),
|
|
116
|
-
environment: firstString(event.environment, metadata.environment),
|
|
117
|
-
});
|
|
137
|
+
return buildDefinedRecord(Object.assign({ funnel_id: firstString(FUNNEL_ID, metadata.funnelId, metadata.funnel_id), funnel_version_id: firstString(FUNNEL_VERSION_ID, metadata.funnelVersionId, metadata.funnel_version_id), project_id: firstString(PROJECT_ID, metadata.projectId, metadata.project_id), step_type: firstString(metadata.stepType, metadata.step_type), environment: firstString(event.environment, metadata.environment) }, readExperimentExposureProperties(GA4_PARAMETER_VALUE_LIMIT, FUNNEL_ID)));
|
|
118
138
|
};
|
|
119
139
|
export const mapAnalyticsEventToGoogleTagEvent = (event) => {
|
|
120
140
|
const googleTagEvent = mapAnalyticsEventToGoogleTagEventParameters(event);
|
|
@@ -232,7 +252,7 @@ export class GoogleTagService {
|
|
|
232
252
|
typeof window !== 'undefined');
|
|
233
253
|
}
|
|
234
254
|
ensureInitialized() {
|
|
235
|
-
var _a, _b, _c, _d;
|
|
255
|
+
var _a, _b, _c, _d, _e;
|
|
236
256
|
if (this.initialized) {
|
|
237
257
|
return;
|
|
238
258
|
}
|
|
@@ -242,18 +262,17 @@ export class GoogleTagService {
|
|
|
242
262
|
if (window.__funnelsgroveGoogleTagHeadReady) {
|
|
243
263
|
return;
|
|
244
264
|
}
|
|
265
|
+
const funnelParameters = readGoogleTagFunnelParameters(FUNNEL_ID, FUNNEL_VERSION_ID);
|
|
245
266
|
if (GOOGLE_TAG_SCRIPT_ID) {
|
|
246
267
|
this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_TAG_SCRIPT_ID));
|
|
247
268
|
(_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
|
|
248
269
|
for (const googleTagConfigId of GOOGLE_TAG_CONFIG_IDS) {
|
|
249
|
-
(_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', googleTagConfigId, {
|
|
250
|
-
page_location: sanitizeAnalyticsUrl((_c = window.location) === null || _c === void 0 ? void 0 : _c.href),
|
|
251
|
-
send_page_view: false,
|
|
252
|
-
});
|
|
270
|
+
(_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', googleTagConfigId, Object.assign({ page_location: sanitizeAnalyticsUrl((_c = window.location) === null || _c === void 0 ? void 0 : _c.href), send_page_view: false }, (googleTagConfigId === GOOGLE_ANALYTICS_ID ? funnelParameters : {})));
|
|
253
271
|
}
|
|
254
272
|
}
|
|
255
273
|
if (GOOGLE_TAG_MANAGER_ID) {
|
|
256
|
-
(_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push(
|
|
274
|
+
(_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push(funnelParameters);
|
|
275
|
+
(_e = window.dataLayer) === null || _e === void 0 ? void 0 : _e.push({
|
|
257
276
|
'gtm.start': Date.now(),
|
|
258
277
|
event: 'gtm.js',
|
|
259
278
|
});
|
|
@@ -25,8 +25,8 @@ const buildCommerceCustomData = (metadata = {}) => {
|
|
|
25
25
|
};
|
|
26
26
|
export const mapAnalyticsEventToMetaPixelEvent = (event) => {
|
|
27
27
|
switch (event.eventType) {
|
|
28
|
+
// One ViewContent per first-step visit: a click on the same step is not a second view.
|
|
28
29
|
case 'first_step_viewed':
|
|
29
|
-
case 'first_step_clicked':
|
|
30
30
|
return {
|
|
31
31
|
eventName: 'ViewContent',
|
|
32
32
|
eventId: event.eventId,
|
|
@@ -2,7 +2,7 @@ import { RudderstackDelivery } from './rudderstack-delivery.js';
|
|
|
2
2
|
import { AmplitudeDelivery } from './amplitude-delivery.js';
|
|
3
3
|
import { toPostHogEventName } from './analytics-export.js';
|
|
4
4
|
import { PostHogDelivery } from './posthog-delivery.js';
|
|
5
|
-
import { FUNNEL_ANALYTICS_EVENT_CONTRACTS, funnelSdkService, getPaywallUser, readAdTrackerClickIdProperties, readCrossFunnelEventProperties, FUNNEL_STEP_CONTRACTS, } from '@funnelsgrove/runtime';
|
|
5
|
+
import { FUNNEL_ANALYTICS_EVENT_CONTRACTS, funnelSdkService, getPaywallUser, readAdTrackerClickIdProperties, readCrossFunnelEventProperties, readExperimentExposureProperties, FUNNEL_STEP_CONTRACTS, } from '@funnelsgrove/runtime';
|
|
6
6
|
import { APP_DEALS_API_BASE_URL, FUNNEL_ID, FUNNEL_VERSION_ID, getFunnelSdkPublishableKey, PROJECT_ID, } from './runtime-api.config.js';
|
|
7
7
|
import { canUseDom, getOrCreateUserId, persistUserId, readStoredUserId, } from './sdk-user-id.storage.js';
|
|
8
8
|
import { mapAnalyticsEventToMetaPixelEvent, metaPixelService, } from './meta-pixel.service.js';
|
|
@@ -129,6 +129,8 @@ const normalizeFeatureFlags = (featureFlags) => {
|
|
|
129
129
|
const buildFeatureFlagProperties = (featureFlags) => {
|
|
130
130
|
return Object.fromEntries(Object.entries(featureFlags).map(([key, value]) => [`$feature/${key}`, value]));
|
|
131
131
|
};
|
|
132
|
+
// Matches the Stripe metadata value limit so PostHog and Stripe carry the same `experiments` JSON.
|
|
133
|
+
const EXPERIMENT_EXPOSURE_VALUE_LIMIT = 500;
|
|
132
134
|
const META_SERVER_EVENT_TYPES = new Set([
|
|
133
135
|
'email_captured',
|
|
134
136
|
'checkout_started',
|
|
@@ -574,9 +576,9 @@ export class PublicAnalyticsSdkService {
|
|
|
574
576
|
const eventEnvironment = event.environment || analyticsEnvironment;
|
|
575
577
|
return {
|
|
576
578
|
event: toPostHogEventName(event.eventType),
|
|
577
|
-
properties: buildDefinedRecord(Object.assign(Object.assign(Object.assign(Object.assign({ distinct_id: ((_a = getPaywallUser()) === null || _a === void 0 ? void 0 : _a.user_id) || event.userId || this.ensureUserId(), $groups: {
|
|
579
|
+
properties: buildDefinedRecord(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ distinct_id: ((_a = getPaywallUser()) === null || _a === void 0 ? void 0 : _a.user_id) || event.userId || this.ensureUserId(), $groups: {
|
|
578
580
|
project: PROJECT_ID,
|
|
579
|
-
}, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: eventEnvironment, test: eventEnvironment === 'test' || undefined, step_id: event.stepId || undefined, step_name: event.stepName || undefined, step_type: event.stepType || undefined, started_at: event.startedAt || undefined, ended_at: event.endedAt || undefined, duration_ms: (_b = event.durationMs) !== null && _b !== void 0 ? _b : undefined, exit_reason: event.exitReason || undefined, step_contract_version: (_c = event.stepContractVersion) !== null && _c !== void 0 ? _c : undefined, engagement_threshold_ms: (_d = event.engagementThresholdMs) !== null && _d !== void 0 ? _d : undefined, user_agent: currentBrowserUserAgent(), event_source: 'client' }, readAdTrackerClickIdProperties()), readCrossFunnelEventProperties(event.metadata.funnel_first_step_id)), buildFeatureFlagProperties(event.featureFlags)), { selected: Object.keys(event.selected).length > 0 ? event.selected : undefined, payload: Object.keys(event.payload).length > 0 ? event.payload : undefined, context: Object.keys(event.context).length > 0 ? event.context : undefined, metadata: Object.keys(event.metadata).length > 0 ? event.metadata : undefined })),
|
|
581
|
+
}, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: eventEnvironment, test: eventEnvironment === 'test' || undefined, step_id: event.stepId || undefined, step_name: event.stepName || undefined, step_type: event.stepType || undefined, started_at: event.startedAt || undefined, ended_at: event.endedAt || undefined, duration_ms: (_b = event.durationMs) !== null && _b !== void 0 ? _b : undefined, exit_reason: event.exitReason || undefined, step_contract_version: (_c = event.stepContractVersion) !== null && _c !== void 0 ? _c : undefined, engagement_threshold_ms: (_d = event.engagementThresholdMs) !== null && _d !== void 0 ? _d : undefined, user_agent: currentBrowserUserAgent(), event_source: 'client' }, readAdTrackerClickIdProperties()), readCrossFunnelEventProperties(event.metadata.funnel_first_step_id)), readExperimentExposureProperties(EXPERIMENT_EXPOSURE_VALUE_LIMIT, FUNNEL_ID)), buildFeatureFlagProperties(event.featureFlags)), { selected: Object.keys(event.selected).length > 0 ? event.selected : undefined, payload: Object.keys(event.payload).length > 0 ? event.payload : undefined, context: Object.keys(event.context).length > 0 ? event.context : undefined, metadata: Object.keys(event.metadata).length > 0 ? event.metadata : undefined })),
|
|
580
582
|
timestamp: event.occurredAt,
|
|
581
583
|
};
|
|
582
584
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funnelsgrove/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"@types/node": "^20",
|
|
46
46
|
"typescript": "^5",
|
|
47
47
|
"vitest": "^3.2.4",
|
|
48
|
-
"@funnelsgrove/runtime": "0.
|
|
48
|
+
"@funnelsgrove/runtime": "0.48.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@funnelsgrove/runtime": "^0.
|
|
51
|
+
"@funnelsgrove/runtime": "^0.48.0"
|
|
52
52
|
}
|
|
53
53
|
}
|