@jitsu/js 1.3.2 → 1.6.1
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/.turbo/turbo-build.log +105 -95
- package/.turbo/turbo-clean.log +5 -5
- package/.turbo/turbo-test.log +126 -0
- package/README.md +9 -0
- package/dist/analytics-plugin.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/jitsu.cjs.js +170 -56
- package/dist/jitsu.es.js +170 -57
- package/dist/web/p.js.txt +1 -1
- package/package.json +2 -2
- package/src/analytics-plugin.ts +57 -16
- package/src/destination-plugins/ga4.ts +123 -0
- package/src/destination-plugins/gtm.ts +39 -38
- package/src/destination-plugins/index.ts +3 -1
- package/src/index.ts +1 -0
- package/tsconfig.json +1 -1
|
@@ -5,12 +5,12 @@ import { applyFilters, CommonDestinationCredentials, InternalPlugin } from "./in
|
|
|
5
5
|
const defaultScriptSrc = "https://www.googletagmanager.com/gtag/js";
|
|
6
6
|
|
|
7
7
|
export type GtmDestinationCredentials = {
|
|
8
|
-
debug
|
|
9
|
-
containerId
|
|
10
|
-
dataLayerName
|
|
11
|
-
preview
|
|
12
|
-
auth
|
|
13
|
-
customScriptSrc
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
containerId?: string;
|
|
10
|
+
dataLayerName?: string;
|
|
11
|
+
preview?: string;
|
|
12
|
+
auth?: string;
|
|
13
|
+
customScriptSrc?: string;
|
|
14
14
|
} & CommonDestinationCredentials;
|
|
15
15
|
|
|
16
16
|
export const gtmPlugin: InternalPlugin<GtmDestinationCredentials> = {
|
|
@@ -19,53 +19,50 @@ export const gtmPlugin: InternalPlugin<GtmDestinationCredentials> = {
|
|
|
19
19
|
if (!applyFilters(payload, config)) {
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
|
-
await initGtmIfNeeded(config);
|
|
22
|
+
await initGtmIfNeeded(config, payload);
|
|
23
23
|
|
|
24
24
|
const dataLayer = window[config.dataLayerName || "dataLayer"];
|
|
25
|
-
|
|
25
|
+
const ids = {
|
|
26
|
+
...(payload.userId ? { user_id: payload.userId, userId: payload.userId } : {}),
|
|
27
|
+
...(payload.anonymousId ? { anonymousId: payload.anonymousId } : {}),
|
|
28
|
+
};
|
|
26
29
|
switch (payload.type) {
|
|
27
30
|
case "page":
|
|
28
31
|
const { properties: pageProperties, context } = payload;
|
|
29
32
|
const pageEvent = {
|
|
30
33
|
event: "page_view",
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
page_location: pageProperties.url,
|
|
35
|
+
page_title: pageProperties.title,
|
|
36
|
+
page_path: pageProperties.path,
|
|
37
|
+
page_hash: pageProperties.hash,
|
|
38
|
+
page_search: pageProperties.search,
|
|
39
|
+
page_referrer: context?.page?.referrer ?? "",
|
|
40
|
+
...ids,
|
|
34
41
|
};
|
|
35
|
-
if (config.debug) {
|
|
36
|
-
console.log("gtag push", pageEvent);
|
|
37
|
-
}
|
|
38
42
|
dataLayer.push(pageEvent);
|
|
39
43
|
break;
|
|
40
44
|
case "track":
|
|
41
45
|
const { properties: trackProperties } = payload;
|
|
42
|
-
const trackEvent: any = {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
trackEvent.anonymousId = payload.anonymousId;
|
|
48
|
-
}
|
|
49
|
-
if (config.debug) {
|
|
50
|
-
console.log("gtag push", trackEvent);
|
|
51
|
-
}
|
|
46
|
+
const trackEvent: any = {
|
|
47
|
+
event: payload.event,
|
|
48
|
+
...trackProperties,
|
|
49
|
+
...ids,
|
|
50
|
+
};
|
|
52
51
|
dataLayer.push(trackEvent);
|
|
53
52
|
break;
|
|
54
53
|
case "identify":
|
|
55
54
|
const { traits } = payload;
|
|
56
|
-
const identifyEvent: any = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
identifyEvent.anonymousId = payload.anonymousId;
|
|
62
|
-
}
|
|
63
|
-
if (config.debug) {
|
|
64
|
-
console.log("gtag push", identifyEvent);
|
|
65
|
-
}
|
|
55
|
+
const identifyEvent: any = {
|
|
56
|
+
event: "identify",
|
|
57
|
+
...traits,
|
|
58
|
+
...ids,
|
|
59
|
+
};
|
|
66
60
|
dataLayer.push(identifyEvent);
|
|
67
61
|
break;
|
|
68
62
|
}
|
|
63
|
+
dataLayer.push(function () {
|
|
64
|
+
this.reset();
|
|
65
|
+
});
|
|
69
66
|
},
|
|
70
67
|
};
|
|
71
68
|
|
|
@@ -79,7 +76,7 @@ function setGtmState(s: GtmState) {
|
|
|
79
76
|
window["__jitsuGtmState"] = s;
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
async function initGtmIfNeeded(config: GtmDestinationCredentials) {
|
|
79
|
+
async function initGtmIfNeeded(config: GtmDestinationCredentials, payload: AnalyticsClientEvent) {
|
|
83
80
|
if (getGtmState() !== "fresh") {
|
|
84
81
|
return;
|
|
85
82
|
}
|
|
@@ -90,23 +87,27 @@ async function initGtmIfNeeded(config: GtmDestinationCredentials) {
|
|
|
90
87
|
const previewParams = config.preview
|
|
91
88
|
? `>m_preview=${config.preview}>m_auth=${config.auth}>m_cookies_win=x`
|
|
92
89
|
: "";
|
|
93
|
-
const
|
|
90
|
+
const tagId = config.containerId;
|
|
91
|
+
const scriptSrc = `${config.customScriptSrc || defaultScriptSrc}?id=${tagId}${dlParam}${previewParams}`;
|
|
94
92
|
|
|
95
93
|
window[dlName] = window[dlName] || [];
|
|
96
94
|
const gtag = function () {
|
|
97
95
|
window[dlName].push(arguments);
|
|
98
96
|
};
|
|
97
|
+
window[dlName].push({
|
|
98
|
+
user_id: payload.userId,
|
|
99
|
+
});
|
|
99
100
|
// @ts-ignore
|
|
100
101
|
gtag("js", new Date());
|
|
101
102
|
// @ts-ignore
|
|
102
|
-
gtag("config",
|
|
103
|
+
gtag("config", tagId);
|
|
103
104
|
|
|
104
105
|
loadScript(scriptSrc)
|
|
105
106
|
.then(() => {
|
|
106
107
|
setGtmState("loaded");
|
|
107
108
|
})
|
|
108
109
|
.catch(e => {
|
|
109
|
-
console.warn(`GTM (containerId=${
|
|
110
|
+
console.warn(`GTM (containerId=${tagId}) init failed: ${e.message}`, e);
|
|
110
111
|
setGtmState("failed");
|
|
111
112
|
});
|
|
112
113
|
}
|
|
@@ -2,6 +2,7 @@ import { AnalyticsClientEvent } from "@jitsu/protocols/analytics";
|
|
|
2
2
|
import { tagPlugin } from "./tag";
|
|
3
3
|
import { logrocketPlugin } from "./logrocket";
|
|
4
4
|
import { gtmPlugin } from "./gtm";
|
|
5
|
+
import { ga4Plugin } from "./ga4";
|
|
5
6
|
|
|
6
7
|
export type InternalPlugin<T> = {
|
|
7
8
|
id: string;
|
|
@@ -36,7 +37,7 @@ export function applyFilters(event: AnalyticsClientEvent, creds: CommonDestinati
|
|
|
36
37
|
const eventsArray = Array.isArray(events) ? events : events.split("\n");
|
|
37
38
|
const hostsArray = Array.isArray(hosts) ? hosts : hosts.split("\n");
|
|
38
39
|
return (
|
|
39
|
-
!!hostsArray.find(hostFilter => satisfyDomainFilter(hostFilter, event.context?.host)) &&
|
|
40
|
+
!!hostsArray.find(hostFilter => satisfyDomainFilter(hostFilter, event.context?.page?.host)) &&
|
|
40
41
|
(!!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.type)) ||
|
|
41
42
|
!!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.event)))
|
|
42
43
|
);
|
|
@@ -55,5 +56,6 @@ export function applyFilters(event: AnalyticsClientEvent, creds: CommonDestinati
|
|
|
55
56
|
export const internalDestinationPlugins: Record<string, InternalPlugin<any>> = {
|
|
56
57
|
[tagPlugin.id]: tagPlugin,
|
|
57
58
|
[gtmPlugin.id]: gtmPlugin,
|
|
59
|
+
[ga4Plugin.id]: ga4Plugin,
|
|
58
60
|
[logrocketPlugin.id]: logrocketPlugin,
|
|
59
61
|
};
|
package/src/index.ts
CHANGED