react-tracker-sdk 1.0.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.
- package/LICENSE +21 -0
- package/README.md +191 -0
- package/dist/TrackerContext.d.ts +4 -0
- package/dist/TrackerContext.js +6 -0
- package/dist/TrackerProvider.d.ts +3 -0
- package/dist/TrackerProvider.js +39 -0
- package/dist/common.d.ts +14 -0
- package/dist/common.js +70 -0
- package/dist/hooks.d.ts +8 -0
- package/dist/hooks.js +20 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +133 -0
- package/dist/matchPath.d.ts +5 -0
- package/dist/matchPath.js +59 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.js +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vuong DQ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# React Tracker SDK
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install --save react-tracker-sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### Simple
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import ReactTracker from "react-tracker-sdk";
|
|
15
|
+
|
|
16
|
+
const reactTracker = new ReactTracker({
|
|
17
|
+
// Configure your tracker server and site by providing
|
|
18
|
+
host: "https://tracking.example.com",
|
|
19
|
+
urlServeJsFile:
|
|
20
|
+
"https://tracking.example.com/tracker/dist/v2/tracker.full.min.js",
|
|
21
|
+
appId: "chat-tool",
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// BAD if app use IAM
|
|
25
|
+
const newHistory = reactTracker.connectToHistory(history);
|
|
26
|
+
ReactDOM.render(
|
|
27
|
+
<Provider store={store}>
|
|
28
|
+
<Router routes={routes} history={newHistory} />
|
|
29
|
+
</Provider>,
|
|
30
|
+
document.getElementById("root")
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// GOOD if app use IAM
|
|
34
|
+
ReactDOM.render(
|
|
35
|
+
<Provider store={store}>
|
|
36
|
+
<Router routes={routes} history={reactTracker.connectToHistory(history)} />
|
|
37
|
+
</Provider>,
|
|
38
|
+
document.getElementById("root")
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Advance
|
|
43
|
+
|
|
44
|
+
#### Auto detect
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import ReactTracker, {
|
|
48
|
+
TrackerProvider,
|
|
49
|
+
useAutoPageView,
|
|
50
|
+
} from "react-tracker-sdk";
|
|
51
|
+
|
|
52
|
+
const reactTracker = new ReactTracker({
|
|
53
|
+
// Configure your tracker server and site by providing
|
|
54
|
+
host: "https://tracking.example.com",
|
|
55
|
+
urlServeJsFile:
|
|
56
|
+
"https://tracking.example.com/tracker/dist/v2/tracker.full.min.js",
|
|
57
|
+
appId: "chat-tool",
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// MUST BE BEFORE EVERYTHING IF USER LOGIN
|
|
61
|
+
window.track("setUserId", "1023912");
|
|
62
|
+
|
|
63
|
+
// ENABLE CONTENT EVENT
|
|
64
|
+
window.track("enableTrackVisibleContentImpressions");
|
|
65
|
+
|
|
66
|
+
const routes = [
|
|
67
|
+
{
|
|
68
|
+
path: "/",
|
|
69
|
+
contentType: "Home",
|
|
70
|
+
exact: true,
|
|
71
|
+
screenName: "dashboard",
|
|
72
|
+
component: Home,
|
|
73
|
+
},
|
|
74
|
+
{ path: "/admin", contentType: "Admin", exact: true, component: Admin },
|
|
75
|
+
{ path: "/logs", contentType: "Log", exact: true, component: Logs },
|
|
76
|
+
{
|
|
77
|
+
path: "/logs/:id",
|
|
78
|
+
contentType: "Detail",
|
|
79
|
+
component: LogDetail,
|
|
80
|
+
// For detail want to extract skuId and skuName
|
|
81
|
+
parser: (url, match) => {
|
|
82
|
+
return { skuId: match.params.id };
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
ReactDOM.render(
|
|
88
|
+
<Provider store={store}>
|
|
89
|
+
<Router history={reactTracker.connectToHistory(history, routes)}>
|
|
90
|
+
{routes.map((route) => (
|
|
91
|
+
<Route {...route} key={route.path} />
|
|
92
|
+
))}
|
|
93
|
+
</Router>
|
|
94
|
+
</Provider>,
|
|
95
|
+
document.getElementById("root")
|
|
96
|
+
);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Support Hook define per screen
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import ReactTracker, {
|
|
103
|
+
TrackerProvider,
|
|
104
|
+
useAutoPageView,
|
|
105
|
+
} from "react-tracker-sdk";
|
|
106
|
+
|
|
107
|
+
const reactTracker = new ReactTracker({
|
|
108
|
+
// Configure your tracker server and site by providing
|
|
109
|
+
host: "https://tracking.example.com",
|
|
110
|
+
urlServeJsFile:
|
|
111
|
+
"https://tracking.example.com/tracker/dist/v2/tracker.full.min.js",
|
|
112
|
+
appId: "chat-tool",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Auto detect pageView
|
|
116
|
+
const ScreenA = (props) => {
|
|
117
|
+
track("setUserId", "random-user-id");
|
|
118
|
+
useAutoPageView({ screenName: "ScreenA" });
|
|
119
|
+
return <>ScreenA</>;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Custom detect pageView
|
|
123
|
+
const ScreenB = (props) => {
|
|
124
|
+
const { callTrackLoadPage, callTrackUnLoadPage } = useTrackPageView();
|
|
125
|
+
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
track("setUserId", "random-user-id");
|
|
128
|
+
// some logic ....
|
|
129
|
+
callTrackLoadPage({ screenName: "ScreenB" });
|
|
130
|
+
return () => {
|
|
131
|
+
// some logic ....
|
|
132
|
+
callTrackUnLoadPage({ screenName: "ScreenB" });
|
|
133
|
+
};
|
|
134
|
+
}, []);
|
|
135
|
+
return <>ScreenA</>;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
ReactDOM.render(
|
|
139
|
+
<Provider store={store}>
|
|
140
|
+
<TrackerProvider history={browserHistory}>
|
|
141
|
+
<Router routes={routes} history={history} />
|
|
142
|
+
</TrackerProvider>
|
|
143
|
+
</Provider>,
|
|
144
|
+
document.getElementById("root")
|
|
145
|
+
);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Advance with content
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
import { TrackerProvider, useAutoPageView } from "react-tracker-sdk";
|
|
152
|
+
|
|
153
|
+
new ReactTracker({
|
|
154
|
+
// Configure your tracker server and site by providing
|
|
155
|
+
host: "https://tracking.example.com",
|
|
156
|
+
urlServeJsFile:
|
|
157
|
+
"https://tracking.example.com/tracker/dist/v2/tracker.full.min.js",
|
|
158
|
+
appId: "chat-tool",
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Custom detect pageView
|
|
162
|
+
const ScreenB = (props) => {
|
|
163
|
+
const { callTrackLoadPage, callTrackUnLoadPage } = useTrackPageView();
|
|
164
|
+
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
track("setUserId", "random-user-id");
|
|
167
|
+
// some logic ....
|
|
168
|
+
callTrackLoadPage({ screenName: "ScreenB" });
|
|
169
|
+
track("enableTrackVisibleContentImpressions");
|
|
170
|
+
return () => {
|
|
171
|
+
// some logic ....
|
|
172
|
+
track("disableTrackVisibleContentImpressions");
|
|
173
|
+
callTrackUnLoadPage({ screenName: "ScreenB" });
|
|
174
|
+
};
|
|
175
|
+
}, []);
|
|
176
|
+
return <>ScreenA</>;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
ReactDOM.render(
|
|
180
|
+
<Provider store={store}>
|
|
181
|
+
<TrackerProvider history={browserHistory}>
|
|
182
|
+
<Router routes={routes} history={history} />
|
|
183
|
+
</TrackerProvider>
|
|
184
|
+
</Provider>,
|
|
185
|
+
document.getElementById("root")
|
|
186
|
+
);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
[MIT](http://opensource.org/licenses/MIT)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import TrackerContext from "./TrackerContext";
|
|
4
|
+
import { getPropsPageView, getFullPath, getPreviousFullPath } from "./common";
|
|
5
|
+
var mPrevLoc;
|
|
6
|
+
var loc = null;
|
|
7
|
+
var unregister = null;
|
|
8
|
+
export var TrackerProvider = function (_a) {
|
|
9
|
+
var children = _a.children, history = _a.history;
|
|
10
|
+
if (!loc) {
|
|
11
|
+
loc = history.location;
|
|
12
|
+
}
|
|
13
|
+
if (!unregister) {
|
|
14
|
+
unregister = history.listen(function (newLoc) {
|
|
15
|
+
loc = newLoc;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
useEffect(function () {
|
|
19
|
+
return function () {
|
|
20
|
+
if (unregister) {
|
|
21
|
+
unregister();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}, [history]);
|
|
25
|
+
var callTrackLoadPage = function (props) {
|
|
26
|
+
var previousFullPath = getPreviousFullPath(mPrevLoc, loc);
|
|
27
|
+
var currentFullPath = getFullPath(loc);
|
|
28
|
+
window.track("setReferrerUrl", previousFullPath);
|
|
29
|
+
window.track("setCurrentUrl", currentFullPath);
|
|
30
|
+
window.track("trackLoadPageView", getPropsPageView(props));
|
|
31
|
+
mPrevLoc = loc;
|
|
32
|
+
};
|
|
33
|
+
var callTrackUnLoadPage = function (props) {
|
|
34
|
+
var previousFullPath = getFullPath(mPrevLoc || loc);
|
|
35
|
+
window.track("setCurrentUrl", previousFullPath);
|
|
36
|
+
window.track("trackUnLoadPageView", getPropsPageView(props));
|
|
37
|
+
};
|
|
38
|
+
return (React.createElement(TrackerContext.Provider, { value: { callTrackLoadPage: callTrackLoadPage, callTrackUnLoadPage: callTrackUnLoadPage } }, children));
|
|
39
|
+
};
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { UseTrackPageViewT } from "./types";
|
|
2
|
+
import { Location } from "history";
|
|
3
|
+
export declare const getProtocal: (loc: any) => any;
|
|
4
|
+
export declare const getPath: (loc: any) => string;
|
|
5
|
+
export declare const getFullPath: (loc: Location) => string;
|
|
6
|
+
export declare const getPropsPageView: (props: UseTrackPageViewT) => {
|
|
7
|
+
contentType?: string | undefined;
|
|
8
|
+
skuId?: string | undefined;
|
|
9
|
+
skuName?: string | undefined;
|
|
10
|
+
screenName?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare const getLocationFromString: (href: string) => HTMLAnchorElement;
|
|
13
|
+
export declare const getPreviousFullPath: (prevLoc: any, currentLoc: any) => string;
|
|
14
|
+
export declare const getMatchRoute: (path: string, routes: any) => any;
|
package/dist/common.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import matchPath from "./matchPath";
|
|
13
|
+
export var getProtocal = function (loc) {
|
|
14
|
+
// Protocol may or may not contain a colon
|
|
15
|
+
var protocol = loc.protocol;
|
|
16
|
+
if (protocol.slice(-1) !== ":") {
|
|
17
|
+
protocol += ":";
|
|
18
|
+
}
|
|
19
|
+
return protocol;
|
|
20
|
+
};
|
|
21
|
+
export var getPath = function (loc) {
|
|
22
|
+
var _loc = window.location;
|
|
23
|
+
var protocol = getProtocal(_loc);
|
|
24
|
+
return protocol + "//" + _loc.host + loc.pathname;
|
|
25
|
+
};
|
|
26
|
+
export var getFullPath = function (loc) {
|
|
27
|
+
var windowLoc = window.location;
|
|
28
|
+
return "".concat(getProtocal(windowLoc), "//").concat(windowLoc.host).concat(loc.pathname).concat(loc.search).concat(loc.hash);
|
|
29
|
+
};
|
|
30
|
+
export var getPropsPageView = function (props) { return (__assign({}, props)); };
|
|
31
|
+
export var getLocationFromString = function (href) {
|
|
32
|
+
var l = document.createElement("a");
|
|
33
|
+
l.href = href;
|
|
34
|
+
return l;
|
|
35
|
+
};
|
|
36
|
+
export var getPreviousFullPath = function (prevLoc, currentLoc) {
|
|
37
|
+
var previousFullPath = null;
|
|
38
|
+
if (!prevLoc) {
|
|
39
|
+
if (!document.referrer) {
|
|
40
|
+
previousFullPath = getFullPath(currentLoc);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
previousFullPath = document.referrer;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
previousFullPath = getFullPath(prevLoc);
|
|
48
|
+
}
|
|
49
|
+
return previousFullPath;
|
|
50
|
+
};
|
|
51
|
+
export var getMatchRoute = function (path, routes) {
|
|
52
|
+
var pathname = getLocationFromString(path).pathname;
|
|
53
|
+
var matches = routes
|
|
54
|
+
.map(function (route) {
|
|
55
|
+
return {
|
|
56
|
+
match: matchPath(pathname, route),
|
|
57
|
+
route: route,
|
|
58
|
+
};
|
|
59
|
+
})
|
|
60
|
+
.filter(function (_a) {
|
|
61
|
+
var match = _a.match;
|
|
62
|
+
return !!match;
|
|
63
|
+
});
|
|
64
|
+
if (matches && matches.length !== 0) {
|
|
65
|
+
var _a = matches[0], match = _a.match, route = _a.route;
|
|
66
|
+
var others = route.parser ? route.parser(path, match) : {};
|
|
67
|
+
return __assign(__assign({}, route), others);
|
|
68
|
+
}
|
|
69
|
+
return {};
|
|
70
|
+
};
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UseTrackPageViewT } from "./types";
|
|
2
|
+
export declare const useAutoPageView: (props: UseTrackPageViewT) => void;
|
|
3
|
+
export declare const useTrackPageView: () => {
|
|
4
|
+
callTrackLoadPage: (props: UseTrackPageViewT) => void;
|
|
5
|
+
callTrackUnLoadPage: (props: UseTrackPageViewT) => void;
|
|
6
|
+
};
|
|
7
|
+
export * from "./TrackerContext";
|
|
8
|
+
export * from "./TrackerProvider";
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import TrackerContext from "./TrackerContext";
|
|
2
|
+
import { useContext, useEffect } from "react";
|
|
3
|
+
export var useAutoPageView = function (props) {
|
|
4
|
+
var _a = useContext(TrackerContext), callTrackLoadPage = _a.callTrackLoadPage, callTrackUnLoadPage = _a.callTrackUnLoadPage;
|
|
5
|
+
useEffect(function () {
|
|
6
|
+
callTrackLoadPage(props);
|
|
7
|
+
return function () {
|
|
8
|
+
callTrackUnLoadPage(props);
|
|
9
|
+
};
|
|
10
|
+
}, []);
|
|
11
|
+
};
|
|
12
|
+
export var useTrackPageView = function () {
|
|
13
|
+
var _a = useContext(TrackerContext), callTrackLoadPage = _a.callTrackLoadPage, callTrackUnLoadPage = _a.callTrackUnLoadPage;
|
|
14
|
+
return {
|
|
15
|
+
callTrackLoadPage: callTrackLoadPage,
|
|
16
|
+
callTrackUnLoadPage: callTrackUnLoadPage,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export * from "./TrackerContext";
|
|
20
|
+
export * from "./TrackerProvider";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { InitContructor, RouteParamT } from "./types";
|
|
2
|
+
declare class ReactTracker {
|
|
3
|
+
private previousPath;
|
|
4
|
+
private previousFullPath;
|
|
5
|
+
private unlistenFromHistory;
|
|
6
|
+
private history;
|
|
7
|
+
private routes;
|
|
8
|
+
constructor(setupOptions: InitContructor);
|
|
9
|
+
connectToHistory(history: any, routes?: RouteParamT[]): any;
|
|
10
|
+
disconnectFromHistory(): boolean;
|
|
11
|
+
private getExtraAttr;
|
|
12
|
+
private registerListener;
|
|
13
|
+
private track;
|
|
14
|
+
}
|
|
15
|
+
export * from "./hooks";
|
|
16
|
+
export default ReactTracker;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { getPath, getFullPath, getPreviousFullPath, getMatchRoute, } from "./common";
|
|
13
|
+
var defaultOptions = {
|
|
14
|
+
host: "https://tracking.example.com",
|
|
15
|
+
urlServeJsFile: "https://tracking.example.com/track/libs/tracker.full.min.js",
|
|
16
|
+
};
|
|
17
|
+
var init = function (f, b, e, v, i, r, t, s) {
|
|
18
|
+
// Stop if tracker already exists
|
|
19
|
+
if (f[i])
|
|
20
|
+
return;
|
|
21
|
+
// Initialise the 'GlobalTrackerNamespace' array
|
|
22
|
+
f["GlobalTrackerNamespace"] = f["GlobalTrackerNamespace"] || [];
|
|
23
|
+
// Add the new Tracker namespace to the global array so tracker.js can find it
|
|
24
|
+
f["GlobalTrackerNamespace"].push(i);
|
|
25
|
+
// Add endpoint
|
|
26
|
+
f["GlobalTrackerNamespace"].push(r);
|
|
27
|
+
// Create the Snowplow function
|
|
28
|
+
f[i] = function () {
|
|
29
|
+
(f[i].q = f[i].q || []).push(arguments);
|
|
30
|
+
};
|
|
31
|
+
// Initialise the asynchronous queue
|
|
32
|
+
f[i].q = f[i].q || [];
|
|
33
|
+
// Create a new script element
|
|
34
|
+
t = b.createElement(e);
|
|
35
|
+
// The new script should load asynchronously
|
|
36
|
+
t.async = !0;
|
|
37
|
+
// Load Tracker-js
|
|
38
|
+
t.src = v;
|
|
39
|
+
// Get the first script on the page
|
|
40
|
+
s = b.getElementsByTagName(e)[0];
|
|
41
|
+
// Insert the Snowplow script before every other script so it executes as soon as possible
|
|
42
|
+
s.parentNode.insertBefore(t, s);
|
|
43
|
+
// add listener error
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
window.onerror = function (msg, url, lineNo, columnNo, error) {
|
|
46
|
+
f[i]("exception", { msg: msg, error: error });
|
|
47
|
+
return false;
|
|
48
|
+
};
|
|
49
|
+
// add listener onunhandledrejection
|
|
50
|
+
window.onunhandledrejection = function (event) {
|
|
51
|
+
f[i]("exception", {
|
|
52
|
+
msg: event.reason ? event.reason.message : "unknown",
|
|
53
|
+
error: "unhandledrejection",
|
|
54
|
+
});
|
|
55
|
+
return false;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
var ReactTracker = /** @class */ (function () {
|
|
59
|
+
function ReactTracker(setupOptions) {
|
|
60
|
+
var _this = this;
|
|
61
|
+
this.routes = [];
|
|
62
|
+
this.registerListener = function (history) {
|
|
63
|
+
var prevLoc = typeof history.getCurrentLocation === "undefined"
|
|
64
|
+
? history.location
|
|
65
|
+
: history.getCurrentLocation();
|
|
66
|
+
_this.previousPath = getPath(prevLoc);
|
|
67
|
+
_this.previousFullPath = getPreviousFullPath(null, prevLoc);
|
|
68
|
+
window.track("setReferrerUrl", _this.previousFullPath);
|
|
69
|
+
var currentFullPath = document.location.href;
|
|
70
|
+
window.track("trackLoadPageView", __assign({}, _this.getExtraAttr(currentFullPath)));
|
|
71
|
+
_this.unlistenFromHistory = history.listen(function (action) {
|
|
72
|
+
// if users use history v5, Location object will be in action.location,
|
|
73
|
+
// else if users use history v4, action will be a Location object.
|
|
74
|
+
// read more at https://gist.github.com/StringEpsilon/47820cc961c8d82f3b71dc856b5cc616#historylisten
|
|
75
|
+
if (action.location) {
|
|
76
|
+
_this.track(action.location);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
_this.track(action);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
var options = __assign(__assign({}, defaultOptions), setupOptions);
|
|
84
|
+
var host = options.host, urlServeJsFile = options.urlServeJsFile;
|
|
85
|
+
init(window, document, "script", urlServeJsFile, "track", host);
|
|
86
|
+
if (options.appId) {
|
|
87
|
+
window.track("init", options.appId);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
ReactTracker.prototype.connectToHistory = function (history, routes) {
|
|
91
|
+
if (routes === void 0) { routes = []; }
|
|
92
|
+
this.routes = routes;
|
|
93
|
+
if (this.history) {
|
|
94
|
+
return history;
|
|
95
|
+
}
|
|
96
|
+
this.history = history;
|
|
97
|
+
this.registerListener(history);
|
|
98
|
+
return history;
|
|
99
|
+
};
|
|
100
|
+
ReactTracker.prototype.disconnectFromHistory = function () {
|
|
101
|
+
if (this.unlistenFromHistory) {
|
|
102
|
+
this.unlistenFromHistory();
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
};
|
|
107
|
+
ReactTracker.prototype.getExtraAttr = function (currentPath) {
|
|
108
|
+
if (!this.routes || this.routes.length === 0)
|
|
109
|
+
return {};
|
|
110
|
+
var _a = getMatchRoute(currentPath, this.routes) || {}, contentType = _a.contentType, screenName = _a.screenName, skuId = _a.skuId, skuName = _a.skuName;
|
|
111
|
+
return JSON.parse(JSON.stringify({ contentType: contentType, screenName: screenName, skuId: skuId, skuName: skuName }));
|
|
112
|
+
};
|
|
113
|
+
ReactTracker.prototype.track = function (loc) {
|
|
114
|
+
if (typeof window === "undefined") {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
var currentPath = getPath(loc);
|
|
118
|
+
var currentFullPath = getFullPath(loc);
|
|
119
|
+
if (this.previousPath === currentPath) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
window.track("setCurrentUrl", this.previousFullPath);
|
|
123
|
+
window.track("trackUnLoadPageView", __assign({}, this.getExtraAttr(this.previousFullPath)));
|
|
124
|
+
window.track("setReferrerUrl", this.previousFullPath);
|
|
125
|
+
window.track("setCurrentUrl", currentFullPath);
|
|
126
|
+
window.track("trackLoadPageView", __assign({}, this.getExtraAttr(currentFullPath)));
|
|
127
|
+
this.previousPath = currentPath;
|
|
128
|
+
this.previousFullPath = currentFullPath;
|
|
129
|
+
};
|
|
130
|
+
return ReactTracker;
|
|
131
|
+
}());
|
|
132
|
+
export * from "./hooks";
|
|
133
|
+
export default ReactTracker;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// clone function matchPath from react-router-dom v5 to make sure this lib can work with react-router-dom v5 and v6
|
|
2
|
+
// https://github.com/remix-run/react-router/blob/v5.3.4/packages/react-router/modules/matchPath.js
|
|
3
|
+
import pathToRegexp from "path-to-regexp";
|
|
4
|
+
var cache = {};
|
|
5
|
+
var cacheLimit = 10000;
|
|
6
|
+
var cacheCount = 0;
|
|
7
|
+
function compilePath(path, options) {
|
|
8
|
+
var cacheKey = "".concat(options.end).concat(options.strict).concat(options.sensitive);
|
|
9
|
+
var pathCache = cache[cacheKey] || (cache[cacheKey] = {});
|
|
10
|
+
if (pathCache[path])
|
|
11
|
+
return pathCache[path];
|
|
12
|
+
var keys = [];
|
|
13
|
+
var regexp = pathToRegexp(path, keys, options);
|
|
14
|
+
var result = { regexp: regexp, keys: keys };
|
|
15
|
+
if (cacheCount < cacheLimit) {
|
|
16
|
+
pathCache[path] = result;
|
|
17
|
+
cacheCount++;
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Public API for matching a URL pathname to a path.
|
|
23
|
+
*/
|
|
24
|
+
function matchPath(pathname, options) {
|
|
25
|
+
if (options === void 0) { options = {}; }
|
|
26
|
+
if (typeof options === "string" || Array.isArray(options)) {
|
|
27
|
+
options = { path: options };
|
|
28
|
+
}
|
|
29
|
+
var path = options.path, _a = options.exact, exact = _a === void 0 ? false : _a, _b = options.strict, strict = _b === void 0 ? false : _b, _c = options.sensitive, sensitive = _c === void 0 ? false : _c;
|
|
30
|
+
var paths = [].concat(path);
|
|
31
|
+
return paths.reduce(function (matched, path) {
|
|
32
|
+
if (!path && path !== "")
|
|
33
|
+
return null;
|
|
34
|
+
if (matched)
|
|
35
|
+
return matched;
|
|
36
|
+
var _a = compilePath(path, {
|
|
37
|
+
end: exact,
|
|
38
|
+
strict: strict,
|
|
39
|
+
sensitive: sensitive,
|
|
40
|
+
}), regexp = _a.regexp, keys = _a.keys;
|
|
41
|
+
var match = regexp.exec(pathname);
|
|
42
|
+
if (!match)
|
|
43
|
+
return null;
|
|
44
|
+
var url = match[0], values = match.slice(1);
|
|
45
|
+
var isExact = pathname === url;
|
|
46
|
+
if (exact && !isExact)
|
|
47
|
+
return null;
|
|
48
|
+
return {
|
|
49
|
+
path: path,
|
|
50
|
+
url: path === "/" && url === "" ? "/" : url,
|
|
51
|
+
isExact: isExact,
|
|
52
|
+
params: keys.reduce(function (memo, key, index) {
|
|
53
|
+
memo[key.name] = values[index];
|
|
54
|
+
return memo;
|
|
55
|
+
}, {}),
|
|
56
|
+
};
|
|
57
|
+
}, null);
|
|
58
|
+
}
|
|
59
|
+
export default matchPath;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { History } from "history";
|
|
3
|
+
import { PathMatch } from "react-router-dom";
|
|
4
|
+
export interface PropsProviderT {
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
history: History;
|
|
7
|
+
}
|
|
8
|
+
export interface InitContructor {
|
|
9
|
+
appId: string;
|
|
10
|
+
host: string;
|
|
11
|
+
urlServeJsFile: string;
|
|
12
|
+
}
|
|
13
|
+
export interface UseTrackPageViewT {
|
|
14
|
+
contentType?: string;
|
|
15
|
+
skuId?: string;
|
|
16
|
+
skuName?: string;
|
|
17
|
+
screenName?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ParserResponseT {
|
|
20
|
+
skuId?: string;
|
|
21
|
+
skuName?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface RouteParamT {
|
|
24
|
+
path?: string;
|
|
25
|
+
exact?: boolean;
|
|
26
|
+
sensitive?: boolean;
|
|
27
|
+
strict?: boolean;
|
|
28
|
+
screenName?: string;
|
|
29
|
+
contentType?: string;
|
|
30
|
+
parser?: (url: string, match: PathMatch) => ParserResponseT;
|
|
31
|
+
}
|
|
32
|
+
export interface Instance {
|
|
33
|
+
callTrackLoadPage: (props: UseTrackPageViewT) => void;
|
|
34
|
+
callTrackUnLoadPage: (props: UseTrackPageViewT) => void;
|
|
35
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-tracker-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Vuong DQ",
|
|
11
|
+
"description": "React plugin for tracker SDK",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"npm",
|
|
14
|
+
"tracker"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"prepublishOnly": "yarn build"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/history": "^4.7.5",
|
|
22
|
+
"@types/react": "^16.9.43",
|
|
23
|
+
"history": "^5.3.0",
|
|
24
|
+
"husky": "^4.2.5",
|
|
25
|
+
"react-router-dom": "^6.3.0",
|
|
26
|
+
"typescript": "^4.1.2"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"path-to-regexp": "^1.7.0",
|
|
30
|
+
"react": ">=16.8.0",
|
|
31
|
+
"react-router-dom": "^6.3.0"
|
|
32
|
+
},
|
|
33
|
+
"husky": {
|
|
34
|
+
"hooks": {
|
|
35
|
+
"pre-commit": "yarn build && git add ."
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"path-to-regexp": "^1.7.0"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=14.0.0"
|
|
43
|
+
}
|
|
44
|
+
}
|