ninetwo-user-tracking 1.0.6
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.d.mts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +163 -0
- package/dist/index.mjs +134 -0
- package/package.json +38 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface TrackingProviderProps {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
gtmId?: string;
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const TrackingProvider: React.FC<TrackingProviderProps>;
|
|
9
|
+
|
|
10
|
+
interface TrackViewProps {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
eventName: string;
|
|
13
|
+
category?: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
threshold?: number;
|
|
16
|
+
}
|
|
17
|
+
declare const TrackView: React.FC<TrackViewProps>;
|
|
18
|
+
|
|
19
|
+
declare const useAutoTrackClick: (enabled?: boolean) => void;
|
|
20
|
+
|
|
21
|
+
export { TrackView, TrackingProvider, useAutoTrackClick };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface TrackingProviderProps {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
gtmId?: string;
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const TrackingProvider: React.FC<TrackingProviderProps>;
|
|
9
|
+
|
|
10
|
+
interface TrackViewProps {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
eventName: string;
|
|
13
|
+
category?: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
threshold?: number;
|
|
16
|
+
}
|
|
17
|
+
declare const TrackView: React.FC<TrackViewProps>;
|
|
18
|
+
|
|
19
|
+
declare const useAutoTrackClick: (enabled?: boolean) => void;
|
|
20
|
+
|
|
21
|
+
export { TrackView, TrackingProvider, useAutoTrackClick };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
TrackView: () => TrackView,
|
|
24
|
+
TrackingProvider: () => TrackingProvider,
|
|
25
|
+
useAutoTrackClick: () => useAutoTrackClick
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/TrackingProvider.tsx
|
|
30
|
+
var import_react2 = require("react");
|
|
31
|
+
|
|
32
|
+
// src/hooks/useAutoTrackClick.ts
|
|
33
|
+
var import_react = require("react");
|
|
34
|
+
|
|
35
|
+
// src/utils/gtm.ts
|
|
36
|
+
var pushToDataLayer = (props) => {
|
|
37
|
+
if (typeof window === "undefined")
|
|
38
|
+
return;
|
|
39
|
+
const dataLayer = window.dataLayer || [];
|
|
40
|
+
dataLayer.push({
|
|
41
|
+
event: props.event,
|
|
42
|
+
// Nome do evento (ex: 'click_cta')
|
|
43
|
+
event_category: props.category,
|
|
44
|
+
event_label: props.label,
|
|
45
|
+
event_type: props.type,
|
|
46
|
+
interaction_time: (/* @__PURE__ */ new Date()).toISOString()
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/hooks/useAutoTrackClick.ts
|
|
51
|
+
var useAutoTrackClick = (enabled = true) => {
|
|
52
|
+
(0, import_react.useEffect)(() => {
|
|
53
|
+
if (!enabled || typeof document === "undefined")
|
|
54
|
+
return;
|
|
55
|
+
const handleClick = (e) => {
|
|
56
|
+
const target = e.target;
|
|
57
|
+
const trackingElement = target.closest("[data-nt-ut-event]");
|
|
58
|
+
if (trackingElement) {
|
|
59
|
+
const eventName = trackingElement.getAttribute("data-nt-ut-event");
|
|
60
|
+
const category = trackingElement.getAttribute("data-nt-ut-category");
|
|
61
|
+
const label = trackingElement.getAttribute("data-nt-ut-label");
|
|
62
|
+
const type = trackingElement.getAttribute("data-nt-ut-type");
|
|
63
|
+
if (!type || type === "click") {
|
|
64
|
+
const data = {
|
|
65
|
+
event: eventName || "interaction",
|
|
66
|
+
category: category || "general",
|
|
67
|
+
label: label || "",
|
|
68
|
+
type: "click"
|
|
69
|
+
};
|
|
70
|
+
console.log("data =>", data);
|
|
71
|
+
pushToDataLayer(data);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
document.addEventListener("click", handleClick, true);
|
|
76
|
+
return () => {
|
|
77
|
+
document.removeEventListener("click", handleClick, true);
|
|
78
|
+
};
|
|
79
|
+
}, [enabled]);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/TrackingProvider.tsx
|
|
83
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
84
|
+
var TrackingProvider = ({
|
|
85
|
+
children,
|
|
86
|
+
gtmId,
|
|
87
|
+
debug = false
|
|
88
|
+
}) => {
|
|
89
|
+
useAutoTrackClick(true);
|
|
90
|
+
(0, import_react2.useEffect)(() => {
|
|
91
|
+
if (!gtmId || typeof window === "undefined") {
|
|
92
|
+
if (debug && !gtmId)
|
|
93
|
+
console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const scriptId = "ninetwo-gtm-script";
|
|
97
|
+
if (document.getElementById(scriptId)) {
|
|
98
|
+
if (debug)
|
|
99
|
+
console.log("[NineTwo Tracking] Script j\xE1 existente. Ignorando reinje\xE7\xE3o.");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
window.dataLayer = window.dataLayer || [];
|
|
103
|
+
window.dataLayer.push({
|
|
104
|
+
"gtm.start": (/* @__PURE__ */ new Date()).getTime(),
|
|
105
|
+
event: "gtm.js"
|
|
106
|
+
});
|
|
107
|
+
const script = document.createElement("script");
|
|
108
|
+
script.id = scriptId;
|
|
109
|
+
script.async = true;
|
|
110
|
+
script.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
|
|
111
|
+
script.onload = () => {
|
|
112
|
+
if (debug)
|
|
113
|
+
console.log(`[NineTwo Tracking] \u2705 GTM carregado com sucesso! (${gtmId})`);
|
|
114
|
+
};
|
|
115
|
+
script.onerror = () => {
|
|
116
|
+
if (debug)
|
|
117
|
+
console.error("[NineTwo Tracking] \u274C Erro ao carregar GTM. Verifique AdBlockers.");
|
|
118
|
+
};
|
|
119
|
+
document.head.appendChild(script);
|
|
120
|
+
}, [gtmId, debug]);
|
|
121
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/components/TrackView.tsx
|
|
125
|
+
var import_react3 = require("react");
|
|
126
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
127
|
+
var TrackView = ({
|
|
128
|
+
children,
|
|
129
|
+
eventName,
|
|
130
|
+
category,
|
|
131
|
+
label,
|
|
132
|
+
threshold = 0.5
|
|
133
|
+
}) => {
|
|
134
|
+
const ref = (0, import_react3.useRef)(null);
|
|
135
|
+
const [hasTriggered, setHasTriggered] = (0, import_react3.useState)(false);
|
|
136
|
+
(0, import_react3.useEffect)(() => {
|
|
137
|
+
const observer = new IntersectionObserver(
|
|
138
|
+
([entry]) => {
|
|
139
|
+
if (entry.isIntersecting && !hasTriggered) {
|
|
140
|
+
pushToDataLayer({
|
|
141
|
+
event: eventName,
|
|
142
|
+
category,
|
|
143
|
+
label,
|
|
144
|
+
type: "view"
|
|
145
|
+
});
|
|
146
|
+
setHasTriggered(true);
|
|
147
|
+
observer.disconnect();
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
{ threshold }
|
|
151
|
+
);
|
|
152
|
+
if (ref.current)
|
|
153
|
+
observer.observe(ref.current);
|
|
154
|
+
return () => observer.disconnect();
|
|
155
|
+
}, [hasTriggered, eventName, category, label, threshold]);
|
|
156
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ref, style: { display: "contents" }, children });
|
|
157
|
+
};
|
|
158
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
159
|
+
0 && (module.exports = {
|
|
160
|
+
TrackView,
|
|
161
|
+
TrackingProvider,
|
|
162
|
+
useAutoTrackClick
|
|
163
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/TrackingProvider.tsx
|
|
2
|
+
import { useEffect as useEffect2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/hooks/useAutoTrackClick.ts
|
|
5
|
+
import { useEffect } from "react";
|
|
6
|
+
|
|
7
|
+
// src/utils/gtm.ts
|
|
8
|
+
var pushToDataLayer = (props) => {
|
|
9
|
+
if (typeof window === "undefined")
|
|
10
|
+
return;
|
|
11
|
+
const dataLayer = window.dataLayer || [];
|
|
12
|
+
dataLayer.push({
|
|
13
|
+
event: props.event,
|
|
14
|
+
// Nome do evento (ex: 'click_cta')
|
|
15
|
+
event_category: props.category,
|
|
16
|
+
event_label: props.label,
|
|
17
|
+
event_type: props.type,
|
|
18
|
+
interaction_time: (/* @__PURE__ */ new Date()).toISOString()
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/hooks/useAutoTrackClick.ts
|
|
23
|
+
var useAutoTrackClick = (enabled = true) => {
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (!enabled || typeof document === "undefined")
|
|
26
|
+
return;
|
|
27
|
+
const handleClick = (e) => {
|
|
28
|
+
const target = e.target;
|
|
29
|
+
const trackingElement = target.closest("[data-nt-ut-event]");
|
|
30
|
+
if (trackingElement) {
|
|
31
|
+
const eventName = trackingElement.getAttribute("data-nt-ut-event");
|
|
32
|
+
const category = trackingElement.getAttribute("data-nt-ut-category");
|
|
33
|
+
const label = trackingElement.getAttribute("data-nt-ut-label");
|
|
34
|
+
const type = trackingElement.getAttribute("data-nt-ut-type");
|
|
35
|
+
if (!type || type === "click") {
|
|
36
|
+
const data = {
|
|
37
|
+
event: eventName || "interaction",
|
|
38
|
+
category: category || "general",
|
|
39
|
+
label: label || "",
|
|
40
|
+
type: "click"
|
|
41
|
+
};
|
|
42
|
+
console.log("data =>", data);
|
|
43
|
+
pushToDataLayer(data);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
document.addEventListener("click", handleClick, true);
|
|
48
|
+
return () => {
|
|
49
|
+
document.removeEventListener("click", handleClick, true);
|
|
50
|
+
};
|
|
51
|
+
}, [enabled]);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/TrackingProvider.tsx
|
|
55
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
56
|
+
var TrackingProvider = ({
|
|
57
|
+
children,
|
|
58
|
+
gtmId,
|
|
59
|
+
debug = false
|
|
60
|
+
}) => {
|
|
61
|
+
useAutoTrackClick(true);
|
|
62
|
+
useEffect2(() => {
|
|
63
|
+
if (!gtmId || typeof window === "undefined") {
|
|
64
|
+
if (debug && !gtmId)
|
|
65
|
+
console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const scriptId = "ninetwo-gtm-script";
|
|
69
|
+
if (document.getElementById(scriptId)) {
|
|
70
|
+
if (debug)
|
|
71
|
+
console.log("[NineTwo Tracking] Script j\xE1 existente. Ignorando reinje\xE7\xE3o.");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
window.dataLayer = window.dataLayer || [];
|
|
75
|
+
window.dataLayer.push({
|
|
76
|
+
"gtm.start": (/* @__PURE__ */ new Date()).getTime(),
|
|
77
|
+
event: "gtm.js"
|
|
78
|
+
});
|
|
79
|
+
const script = document.createElement("script");
|
|
80
|
+
script.id = scriptId;
|
|
81
|
+
script.async = true;
|
|
82
|
+
script.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
|
|
83
|
+
script.onload = () => {
|
|
84
|
+
if (debug)
|
|
85
|
+
console.log(`[NineTwo Tracking] \u2705 GTM carregado com sucesso! (${gtmId})`);
|
|
86
|
+
};
|
|
87
|
+
script.onerror = () => {
|
|
88
|
+
if (debug)
|
|
89
|
+
console.error("[NineTwo Tracking] \u274C Erro ao carregar GTM. Verifique AdBlockers.");
|
|
90
|
+
};
|
|
91
|
+
document.head.appendChild(script);
|
|
92
|
+
}, [gtmId, debug]);
|
|
93
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/components/TrackView.tsx
|
|
97
|
+
import { useEffect as useEffect3, useRef, useState } from "react";
|
|
98
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
99
|
+
var TrackView = ({
|
|
100
|
+
children,
|
|
101
|
+
eventName,
|
|
102
|
+
category,
|
|
103
|
+
label,
|
|
104
|
+
threshold = 0.5
|
|
105
|
+
}) => {
|
|
106
|
+
const ref = useRef(null);
|
|
107
|
+
const [hasTriggered, setHasTriggered] = useState(false);
|
|
108
|
+
useEffect3(() => {
|
|
109
|
+
const observer = new IntersectionObserver(
|
|
110
|
+
([entry]) => {
|
|
111
|
+
if (entry.isIntersecting && !hasTriggered) {
|
|
112
|
+
pushToDataLayer({
|
|
113
|
+
event: eventName,
|
|
114
|
+
category,
|
|
115
|
+
label,
|
|
116
|
+
type: "view"
|
|
117
|
+
});
|
|
118
|
+
setHasTriggered(true);
|
|
119
|
+
observer.disconnect();
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
{ threshold }
|
|
123
|
+
);
|
|
124
|
+
if (ref.current)
|
|
125
|
+
observer.observe(ref.current);
|
|
126
|
+
return () => observer.disconnect();
|
|
127
|
+
}, [hasTriggered, eventName, category, label, threshold]);
|
|
128
|
+
return /* @__PURE__ */ jsx2("div", { ref, style: { display: "contents" }, children });
|
|
129
|
+
};
|
|
130
|
+
export {
|
|
131
|
+
TrackView,
|
|
132
|
+
TrackingProvider,
|
|
133
|
+
useAutoTrackClick
|
|
134
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ninetwo-user-tracking",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "User tracking abstraction for React/Nextjs with GTM",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
14
|
+
"prepublishOnly": "npm run build"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"react",
|
|
18
|
+
"tracking",
|
|
19
|
+
"gtm",
|
|
20
|
+
"analytics"
|
|
21
|
+
],
|
|
22
|
+
"author": "NineTwo",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": ">=16.8.0",
|
|
26
|
+
"react-dom": ">=16.8.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^20.0.0",
|
|
30
|
+
"@types/react": "^18.0.0",
|
|
31
|
+
"tsup": "^8.0.0",
|
|
32
|
+
"typescript": "^5.0.0"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://guirossetim-M2Z@github.com/guirossetim-M2Z/ninetwo-gtag.git"
|
|
37
|
+
}
|
|
38
|
+
}
|