ninetwo-user-tracking 1.0.9 → 1.0.11
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/dist/index.js +89 -31
- package/dist/index.mjs +89 -31
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -36,14 +36,28 @@ var import_react = require("react");
|
|
|
36
36
|
var pushToDataLayer = (props) => {
|
|
37
37
|
if (typeof window === "undefined")
|
|
38
38
|
return;
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
const { event, category, label, type, ...rest } = props;
|
|
40
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
41
|
+
const params = {
|
|
42
|
+
event_category: category,
|
|
43
|
+
event_label: label,
|
|
44
|
+
event_type: type,
|
|
45
|
+
interaction_time: timestamp,
|
|
46
|
+
...rest
|
|
47
|
+
// permite enviar dados extras
|
|
48
|
+
};
|
|
49
|
+
if (typeof window.gtag === "function") {
|
|
50
|
+
try {
|
|
51
|
+
window.gtag("event", event, params);
|
|
52
|
+
return;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
console.warn("[NineTwo Tracking] gtag falhou, fallback para dataLayer", err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
window.dataLayer = window.dataLayer || [];
|
|
58
|
+
window.dataLayer.push({
|
|
59
|
+
event,
|
|
60
|
+
...params
|
|
47
61
|
});
|
|
48
62
|
};
|
|
49
63
|
|
|
@@ -120,34 +134,78 @@ var TrackingProvider = ({
|
|
|
120
134
|
useAutoTrackSubmit(true);
|
|
121
135
|
(0, import_react3.useEffect)(() => {
|
|
122
136
|
if (!gtmId || typeof window === "undefined") {
|
|
123
|
-
if (debug && !gtmId)
|
|
124
|
-
console.warn("[NineTwo Tracking]
|
|
137
|
+
if (debug && !gtmId) {
|
|
138
|
+
console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
|
|
139
|
+
}
|
|
125
140
|
return;
|
|
126
141
|
}
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
const isGA4 = gtmId.startsWith("G-");
|
|
143
|
+
const isGTM = gtmId.startsWith("GTM-");
|
|
144
|
+
if (!isGA4 && !isGTM) {
|
|
145
|
+
console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const existing = document.querySelector(
|
|
149
|
+
`script[data-ninetwo-tracking="${gtmId}"]`
|
|
150
|
+
);
|
|
151
|
+
if (existing) {
|
|
152
|
+
if (debug) {
|
|
153
|
+
console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
|
|
154
|
+
}
|
|
131
155
|
return;
|
|
132
156
|
}
|
|
133
157
|
window.dataLayer = window.dataLayer || [];
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
console.error("[NineTwo Tracking]
|
|
149
|
-
|
|
150
|
-
|
|
158
|
+
if (isGA4) {
|
|
159
|
+
if (debug) {
|
|
160
|
+
console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
|
|
161
|
+
}
|
|
162
|
+
const gtagScript = document.createElement("script");
|
|
163
|
+
gtagScript.async = true;
|
|
164
|
+
gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
|
|
165
|
+
gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
|
|
166
|
+
gtagScript.onload = () => {
|
|
167
|
+
if (debug) {
|
|
168
|
+
console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
gtagScript.onerror = () => {
|
|
172
|
+
console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
|
|
173
|
+
};
|
|
174
|
+
document.head.appendChild(gtagScript);
|
|
175
|
+
const inlineScript = document.createElement("script");
|
|
176
|
+
inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
|
|
177
|
+
inlineScript.innerHTML = `
|
|
178
|
+
window.dataLayer = window.dataLayer || [];
|
|
179
|
+
function gtag(){dataLayer.push(arguments);}
|
|
180
|
+
gtag('js', new Date());
|
|
181
|
+
gtag('config', '${gtmId}', {
|
|
182
|
+
send_page_view: true
|
|
183
|
+
});
|
|
184
|
+
`;
|
|
185
|
+
document.head.appendChild(inlineScript);
|
|
186
|
+
}
|
|
187
|
+
if (isGTM) {
|
|
188
|
+
if (debug) {
|
|
189
|
+
console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
|
|
190
|
+
}
|
|
191
|
+
window.dataLayer.push({
|
|
192
|
+
"gtm.start": (/* @__PURE__ */ new Date()).getTime(),
|
|
193
|
+
event: "gtm.js"
|
|
194
|
+
});
|
|
195
|
+
const gtmScript = document.createElement("script");
|
|
196
|
+
gtmScript.async = true;
|
|
197
|
+
gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
|
|
198
|
+
gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
|
|
199
|
+
gtmScript.onload = () => {
|
|
200
|
+
if (debug) {
|
|
201
|
+
console.log("[NineTwo Tracking] GTM carregado com sucesso");
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
gtmScript.onerror = () => {
|
|
205
|
+
console.error("[NineTwo Tracking] Erro ao carregar GTM");
|
|
206
|
+
};
|
|
207
|
+
document.head.appendChild(gtmScript);
|
|
208
|
+
}
|
|
151
209
|
}, [gtmId, debug]);
|
|
152
210
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
153
211
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -8,14 +8,28 @@ import { useEffect } from "react";
|
|
|
8
8
|
var pushToDataLayer = (props) => {
|
|
9
9
|
if (typeof window === "undefined")
|
|
10
10
|
return;
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const { event, category, label, type, ...rest } = props;
|
|
12
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
13
|
+
const params = {
|
|
14
|
+
event_category: category,
|
|
15
|
+
event_label: label,
|
|
16
|
+
event_type: type,
|
|
17
|
+
interaction_time: timestamp,
|
|
18
|
+
...rest
|
|
19
|
+
// permite enviar dados extras
|
|
20
|
+
};
|
|
21
|
+
if (typeof window.gtag === "function") {
|
|
22
|
+
try {
|
|
23
|
+
window.gtag("event", event, params);
|
|
24
|
+
return;
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.warn("[NineTwo Tracking] gtag falhou, fallback para dataLayer", err);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
window.dataLayer = window.dataLayer || [];
|
|
30
|
+
window.dataLayer.push({
|
|
31
|
+
event,
|
|
32
|
+
...params
|
|
19
33
|
});
|
|
20
34
|
};
|
|
21
35
|
|
|
@@ -92,34 +106,78 @@ var TrackingProvider = ({
|
|
|
92
106
|
useAutoTrackSubmit(true);
|
|
93
107
|
useEffect3(() => {
|
|
94
108
|
if (!gtmId || typeof window === "undefined") {
|
|
95
|
-
if (debug && !gtmId)
|
|
96
|
-
console.warn("[NineTwo Tracking]
|
|
109
|
+
if (debug && !gtmId) {
|
|
110
|
+
console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const isGA4 = gtmId.startsWith("G-");
|
|
115
|
+
const isGTM = gtmId.startsWith("GTM-");
|
|
116
|
+
if (!isGA4 && !isGTM) {
|
|
117
|
+
console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
|
|
97
118
|
return;
|
|
98
119
|
}
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
120
|
+
const existing = document.querySelector(
|
|
121
|
+
`script[data-ninetwo-tracking="${gtmId}"]`
|
|
122
|
+
);
|
|
123
|
+
if (existing) {
|
|
124
|
+
if (debug) {
|
|
125
|
+
console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
|
|
126
|
+
}
|
|
103
127
|
return;
|
|
104
128
|
}
|
|
105
129
|
window.dataLayer = window.dataLayer || [];
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
console.error("[NineTwo Tracking]
|
|
121
|
-
|
|
122
|
-
|
|
130
|
+
if (isGA4) {
|
|
131
|
+
if (debug) {
|
|
132
|
+
console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
|
|
133
|
+
}
|
|
134
|
+
const gtagScript = document.createElement("script");
|
|
135
|
+
gtagScript.async = true;
|
|
136
|
+
gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
|
|
137
|
+
gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
|
|
138
|
+
gtagScript.onload = () => {
|
|
139
|
+
if (debug) {
|
|
140
|
+
console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
gtagScript.onerror = () => {
|
|
144
|
+
console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
|
|
145
|
+
};
|
|
146
|
+
document.head.appendChild(gtagScript);
|
|
147
|
+
const inlineScript = document.createElement("script");
|
|
148
|
+
inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
|
|
149
|
+
inlineScript.innerHTML = `
|
|
150
|
+
window.dataLayer = window.dataLayer || [];
|
|
151
|
+
function gtag(){dataLayer.push(arguments);}
|
|
152
|
+
gtag('js', new Date());
|
|
153
|
+
gtag('config', '${gtmId}', {
|
|
154
|
+
send_page_view: true
|
|
155
|
+
});
|
|
156
|
+
`;
|
|
157
|
+
document.head.appendChild(inlineScript);
|
|
158
|
+
}
|
|
159
|
+
if (isGTM) {
|
|
160
|
+
if (debug) {
|
|
161
|
+
console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
|
|
162
|
+
}
|
|
163
|
+
window.dataLayer.push({
|
|
164
|
+
"gtm.start": (/* @__PURE__ */ new Date()).getTime(),
|
|
165
|
+
event: "gtm.js"
|
|
166
|
+
});
|
|
167
|
+
const gtmScript = document.createElement("script");
|
|
168
|
+
gtmScript.async = true;
|
|
169
|
+
gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
|
|
170
|
+
gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
|
|
171
|
+
gtmScript.onload = () => {
|
|
172
|
+
if (debug) {
|
|
173
|
+
console.log("[NineTwo Tracking] GTM carregado com sucesso");
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
gtmScript.onerror = () => {
|
|
177
|
+
console.error("[NineTwo Tracking] Erro ao carregar GTM");
|
|
178
|
+
};
|
|
179
|
+
document.head.appendChild(gtmScript);
|
|
180
|
+
}
|
|
123
181
|
}, [gtmId, debug]);
|
|
124
182
|
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
125
183
|
};
|