@funnelsgrove/analytics 0.1.5 → 0.1.7
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,4 +1,9 @@
|
|
|
1
1
|
type GoogleTagEventName = 'purchase';
|
|
2
|
+
export type GoogleTagHeadScript = {
|
|
3
|
+
id: string;
|
|
4
|
+
src?: string;
|
|
5
|
+
content?: string;
|
|
6
|
+
};
|
|
2
7
|
export type GoogleTagTrackInput = {
|
|
3
8
|
eventName: GoogleTagEventName;
|
|
4
9
|
parameters?: Record<string, unknown>;
|
|
@@ -10,11 +15,13 @@ export type AnalyticsEventForGoogleTag = {
|
|
|
10
15
|
};
|
|
11
16
|
declare global {
|
|
12
17
|
interface Window {
|
|
18
|
+
__funnelsgroveGoogleTagHeadReady?: boolean;
|
|
13
19
|
__funnelsgroveGoogleTagServiceReady?: boolean;
|
|
14
20
|
dataLayer?: unknown[];
|
|
15
21
|
gtag?: (...args: unknown[]) => void;
|
|
16
22
|
}
|
|
17
23
|
}
|
|
24
|
+
export declare const buildGoogleTagHeadScripts: () => GoogleTagHeadScript[];
|
|
18
25
|
export declare const mapAnalyticsEventToGoogleTagEvent: (event: AnalyticsEventForGoogleTag) => GoogleTagTrackInput | null;
|
|
19
26
|
export declare class GoogleTagService {
|
|
20
27
|
private initialized;
|
|
@@ -3,6 +3,51 @@ import { buildBrowserPurchaseAnalyticsAttributes } from './purchase-attributes.s
|
|
|
3
3
|
const normalizeEventTarget = (value) => {
|
|
4
4
|
return value === 'dataLayer' || value === 'gtag' || value === 'both' ? value : 'both';
|
|
5
5
|
};
|
|
6
|
+
const buildGoogleAnalyticsScriptSrc = (googleAnalyticsId) => `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(googleAnalyticsId)}`;
|
|
7
|
+
const buildGoogleTagManagerScriptSrc = (googleTagManagerId) => `https://www.googletagmanager.com/gtm.js?id=${encodeURIComponent(googleTagManagerId)}`;
|
|
8
|
+
export const buildGoogleTagHeadScripts = () => {
|
|
9
|
+
if (!GOOGLE_TAG_ENABLED || (!GOOGLE_ANALYTICS_ID && !GOOGLE_TAG_MANAGER_ID)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
const bootstrapLines = [
|
|
13
|
+
'window.dataLayer = window.dataLayer || [];',
|
|
14
|
+
'window.gtag = window.gtag || function(){ window.dataLayer.push(arguments); };',
|
|
15
|
+
'window.__funnelsgroveGoogleTagHeadReady = true;',
|
|
16
|
+
...(GOOGLE_ANALYTICS_ID
|
|
17
|
+
? [
|
|
18
|
+
'window.gtag("js", new Date());',
|
|
19
|
+
`window.gtag("config", ${JSON.stringify(GOOGLE_ANALYTICS_ID)});`,
|
|
20
|
+
]
|
|
21
|
+
: []),
|
|
22
|
+
...(GOOGLE_TAG_MANAGER_ID
|
|
23
|
+
? [
|
|
24
|
+
'window.dataLayer.push({"gtm.start": Date.now(), event: "gtm.js"});',
|
|
25
|
+
]
|
|
26
|
+
: []),
|
|
27
|
+
];
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
id: 'funnelsgrove-google-tag-bootstrap',
|
|
31
|
+
content: bootstrapLines.join('\n'),
|
|
32
|
+
},
|
|
33
|
+
...(GOOGLE_ANALYTICS_ID
|
|
34
|
+
? [
|
|
35
|
+
{
|
|
36
|
+
id: 'funnelsgrove-google-analytics',
|
|
37
|
+
src: buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID),
|
|
38
|
+
},
|
|
39
|
+
]
|
|
40
|
+
: []),
|
|
41
|
+
...(GOOGLE_TAG_MANAGER_ID
|
|
42
|
+
? [
|
|
43
|
+
{
|
|
44
|
+
id: 'funnelsgrove-google-tag-manager',
|
|
45
|
+
src: buildGoogleTagManagerScriptSrc(GOOGLE_TAG_MANAGER_ID),
|
|
46
|
+
},
|
|
47
|
+
]
|
|
48
|
+
: []),
|
|
49
|
+
];
|
|
50
|
+
};
|
|
6
51
|
export const mapAnalyticsEventToGoogleTagEvent = (event) => {
|
|
7
52
|
var _a;
|
|
8
53
|
switch (event.eventType) {
|
|
@@ -53,8 +98,11 @@ export class GoogleTagService {
|
|
|
53
98
|
this.initialized = true;
|
|
54
99
|
window.__funnelsgroveGoogleTagServiceReady = true;
|
|
55
100
|
this.ensureDataLayer();
|
|
101
|
+
if (window.__funnelsgroveGoogleTagHeadReady) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
56
104
|
if (GOOGLE_ANALYTICS_ID) {
|
|
57
|
-
this.installScript(
|
|
105
|
+
this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID));
|
|
58
106
|
(_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
|
|
59
107
|
(_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', GOOGLE_ANALYTICS_ID);
|
|
60
108
|
}
|
|
@@ -63,7 +111,7 @@ export class GoogleTagService {
|
|
|
63
111
|
'gtm.start': Date.now(),
|
|
64
112
|
event: 'gtm.js',
|
|
65
113
|
});
|
|
66
|
-
this.installScript(
|
|
114
|
+
this.installScript(buildGoogleTagManagerScriptSrc(GOOGLE_TAG_MANAGER_ID));
|
|
67
115
|
}
|
|
68
116
|
}
|
|
69
117
|
ensureDataLayer() {
|
|
@@ -74,9 +122,13 @@ export class GoogleTagService {
|
|
|
74
122
|
});
|
|
75
123
|
}
|
|
76
124
|
installScript(src) {
|
|
125
|
+
var _a;
|
|
77
126
|
if (typeof document === 'undefined') {
|
|
78
127
|
return;
|
|
79
128
|
}
|
|
129
|
+
if ((_a = document.querySelector) === null || _a === void 0 ? void 0 : _a.call(document, `script[src="${src}"]`)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
80
132
|
const firstScript = document.getElementsByTagName('script')[0];
|
|
81
133
|
const parent = firstScript === null || firstScript === void 0 ? void 0 : firstScript.parentNode;
|
|
82
134
|
if (!parent) {
|
|
@@ -71,10 +71,11 @@ export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
|
|
|
71
71
|
const document = asRecord(input.funnelUserDocument);
|
|
72
72
|
const attribution = asRecord((_a = input.attribution) !== null && _a !== void 0 ? _a : document.attribution);
|
|
73
73
|
const attributionContext = asRecord(attribution.context);
|
|
74
|
+
const progressAttributes = asRecord(asRecord(document.progress).attributes);
|
|
74
75
|
const transactionInfo = asRecord(document.transactionInfo);
|
|
75
76
|
const amountCents = toNumber(input.amountCents);
|
|
76
77
|
const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
|
|
77
|
-
return buildDefinedRecord(Object.assign(Object.assign({}, normalizeAttributionTouch(attribution.firstTouch)), { currency: toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), user_id: firstString(input.userId, input.user_id), project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), geo_city: firstString(input.geoCity, input.geo_city, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl ? firstString(input.url, currentBrowserUrl()) : undefined }));
|
|
78
|
+
return buildDefinedRecord(Object.assign(Object.assign({}, normalizeAttributionTouch(attribution.firstTouch)), { currency: toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), user_id: firstString(input.userId, input.user_id), project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), timezone: firstString(input.timezone, input.time_zone, progressAttributes.timezone, progressAttributes.time_zone, document.timezone, document.time_zone, attributionContext.timezone, attributionContext.time_zone), geo_city: firstString(input.geoCity, input.geo_city, progressAttributes.geo_city, progressAttributes.geoCity, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, progressAttributes.geo_country, progressAttributes.geoCountry, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl ? firstString(input.url, currentBrowserUrl()) : undefined }));
|
|
78
79
|
};
|
|
79
80
|
export const buildBrowserPurchaseAnalyticsAttributes = (input = {}) => {
|
|
80
81
|
return buildPurchaseAnalyticsAttributes(input, { includeUrl: true });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funnelsgrove/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"test:run": "vitest run --passWithNoTests"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@funnelsgrove/runtime": "0.1.
|
|
29
|
+
"@funnelsgrove/runtime": "0.1.16"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "^20",
|