aesirx-analytics 1.0.0-alpha.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of aesirx-analytics might be problematic. Click here for more details.
- package/.babelrc.js +54 -0
- package/.env.dist +1 -0
- package/.eslintrc +53 -0
- package/.github/workflows/codeql-analysis.yml +48 -0
- package/.github/workflows/npm.yml +29 -0
- package/.prettierrc +10 -0
- package/LICENSE +674 -0
- package/README.md +68 -0
- package/babel.config.json +5 -0
- package/dist/analytics.js +1 -0
- package/dist/lib/bundles/bundle.esm.min.js +2 -0
- package/dist/lib/bundles/bundle.esm.min.js.map +1 -0
- package/dist/lib/cjs/AnalyticsNext/handle.js +124 -0
- package/dist/lib/cjs/AnalyticsNext/handle.js.map +1 -0
- package/dist/lib/cjs/AnalyticsNext/index.js +50 -0
- package/dist/lib/cjs/AnalyticsNext/index.js.map +1 -0
- package/dist/lib/cjs/AnalyticsReact/handle.js +78 -0
- package/dist/lib/cjs/AnalyticsReact/handle.js.map +1 -0
- package/dist/lib/cjs/AnalyticsReact/index.js +50 -0
- package/dist/lib/cjs/AnalyticsReact/index.js.map +1 -0
- package/dist/lib/cjs/analytics.js +167 -0
- package/dist/lib/cjs/analytics.js.map +1 -0
- package/dist/lib/cjs/index.js +21 -0
- package/dist/lib/cjs/index.js.map +1 -0
- package/dist/lib/cjs/utils/AnalyticsContextProvider.js +39 -0
- package/dist/lib/cjs/utils/AnalyticsContextProvider.js.map +1 -0
- package/dist/lib/cjs/utils/index.js +174 -0
- package/dist/lib/cjs/utils/index.js.map +1 -0
- package/dist/lib/cjs/utils/services.js +53 -0
- package/dist/lib/cjs/utils/services.js.map +1 -0
- package/dist/lib/esm/AnalyticsNext/handle.js +129 -0
- package/dist/lib/esm/AnalyticsNext/handle.js.map +1 -0
- package/dist/lib/esm/AnalyticsNext/index.js +55 -0
- package/dist/lib/esm/AnalyticsNext/index.js.map +1 -0
- package/dist/lib/esm/AnalyticsReact/handle.js +83 -0
- package/dist/lib/esm/AnalyticsReact/handle.js.map +1 -0
- package/dist/lib/esm/AnalyticsReact/index.js +55 -0
- package/dist/lib/esm/AnalyticsReact/index.js.map +1 -0
- package/dist/lib/esm/analytics.js +168 -0
- package/dist/lib/esm/analytics.js.map +1 -0
- package/dist/lib/esm/index.js +21 -0
- package/dist/lib/esm/index.js.map +1 -0
- package/dist/lib/esm/utils/AnalyticsContextProvider.js +43 -0
- package/dist/lib/esm/utils/AnalyticsContextProvider.js.map +1 -0
- package/dist/lib/esm/utils/index.js +176 -0
- package/dist/lib/esm/utils/index.js.map +1 -0
- package/dist/lib/esm/utils/services.js +54 -0
- package/dist/lib/esm/utils/services.js.map +1 -0
- package/dist/lib/types/AnalyticsNext/handle.d.ts +3 -0
- package/dist/lib/types/AnalyticsNext/handle.d.ts.map +1 -0
- package/dist/lib/types/AnalyticsNext/index.d.ts +3 -0
- package/dist/lib/types/AnalyticsNext/index.d.ts.map +1 -0
- package/dist/lib/types/AnalyticsReact/handle.d.ts +3 -0
- package/dist/lib/types/AnalyticsReact/handle.d.ts.map +1 -0
- package/dist/lib/types/AnalyticsReact/index.d.ts +3 -0
- package/dist/lib/types/AnalyticsReact/index.d.ts.map +1 -0
- package/dist/lib/types/index.d.ts +3 -0
- package/dist/lib/types/index.d.ts.map +1 -0
- package/dist/lib/types/index.min.js +1 -0
- package/dist/lib/types/utils/AnalyticsContextProvider.d.ts +3 -0
- package/dist/lib/types/utils/AnalyticsContextProvider.d.ts.map +1 -0
- package/dist/lib/types/utils/index.d.ts +4 -0
- package/dist/lib/types/utils/index.d.ts.map +1 -0
- package/dist/lib/types/utils/services.d.ts +2 -0
- package/dist/lib/types/utils/services.d.ts.map +1 -0
- package/package.json +80 -0
- package/rollup.config.mjs +75 -0
- package/src/AnalyticsNext/handle.js +57 -0
- package/src/AnalyticsNext/index.js +30 -0
- package/src/AnalyticsReact/handle.js +39 -0
- package/src/AnalyticsReact/index.js +29 -0
- package/src/analytics.js +112 -0
- package/src/index.js +2 -0
- package/src/utils/AnalyticsContextProvider.js +17 -0
- package/src/utils/index.js +93 -0
- package/src/utils/services.js +20 -0
- package/tsconfig.json +30 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
import React, { useState } from 'react';
|
2
|
+
|
3
|
+
import { AnalyticsContextProvider } from '../utils/AnalyticsContextProvider';
|
4
|
+
import AnalyticsHandle from './handle';
|
5
|
+
const AnalyticsReact = ({ pathname }) => {
|
6
|
+
const [eventID, setEventID] = useState();
|
7
|
+
const [UUID, setUUID] = useState();
|
8
|
+
const [eventIDStart, setEventIDStart] = useState();
|
9
|
+
const [UUIDStart, setUUIDStart] = useState();
|
10
|
+
return (
|
11
|
+
<>
|
12
|
+
<AnalyticsContextProvider
|
13
|
+
value={{
|
14
|
+
event_uuid: eventID,
|
15
|
+
visitor_uuid: UUID,
|
16
|
+
event_uuid_start: eventIDStart,
|
17
|
+
visitor_uuid_start: UUIDStart,
|
18
|
+
setEventID: setEventID,
|
19
|
+
setUUID: setUUID,
|
20
|
+
setEventIDStart: setEventIDStart,
|
21
|
+
setUUIDStart: setUUIDStart,
|
22
|
+
}}
|
23
|
+
>
|
24
|
+
<AnalyticsHandle pathname={pathname} />
|
25
|
+
</AnalyticsContextProvider>
|
26
|
+
</>
|
27
|
+
);
|
28
|
+
};
|
29
|
+
export default AnalyticsReact;
|
package/src/analytics.js
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
import { endTracker, initTracker, startTracker } from './utils';
|
2
|
+
|
3
|
+
const AesirAnalytics = () => {
|
4
|
+
const hook = (_this, method, callback) => {
|
5
|
+
const orig = _this[method];
|
6
|
+
|
7
|
+
return (...args) => {
|
8
|
+
callback.apply(null, args);
|
9
|
+
|
10
|
+
return orig.apply(_this, args);
|
11
|
+
};
|
12
|
+
};
|
13
|
+
|
14
|
+
/* Handle history changes */
|
15
|
+
|
16
|
+
const handlePush = async (url) => {
|
17
|
+
if (!url) return;
|
18
|
+
const { pathname, search, origin } = location;
|
19
|
+
url = `${origin}${pathname}${search}`;
|
20
|
+
const newUrl = url.toString();
|
21
|
+
|
22
|
+
if (newUrl.substring(0, 4) === 'http') {
|
23
|
+
url = '/' + newUrl.split('/').splice(3).join('/');
|
24
|
+
} else {
|
25
|
+
url = newUrl;
|
26
|
+
}
|
27
|
+
|
28
|
+
// if (currentUrl !== currentRef) {
|
29
|
+
// await initTracker();
|
30
|
+
// startTracker();
|
31
|
+
// }
|
32
|
+
};
|
33
|
+
|
34
|
+
/* Global */
|
35
|
+
|
36
|
+
if (!window.tracker) {
|
37
|
+
const tracker = (eventValue) => eventValue;
|
38
|
+
tracker.initTracker = initTracker;
|
39
|
+
tracker.startTracker = startTracker;
|
40
|
+
|
41
|
+
window.tracker = tracker;
|
42
|
+
}
|
43
|
+
|
44
|
+
/* Start */
|
45
|
+
|
46
|
+
history.pushState = hook(history, 'pushState', handlePush);
|
47
|
+
history.replaceState = hook(history, 'replaceState', handlePush);
|
48
|
+
|
49
|
+
const envParams = process.env;
|
50
|
+
const hostUrl = envParams.ENDPOINT_ANALYTICS_URL ? envParams.ENDPOINT_ANALYTICS_URL : '';
|
51
|
+
|
52
|
+
const root = hostUrl ? hostUrl.replace(/\/$/, '') : '';
|
53
|
+
|
54
|
+
const update = async () => {
|
55
|
+
if (document.readyState === 'complete') {
|
56
|
+
const responseInit = await initTracker(root);
|
57
|
+
if (responseInit) {
|
58
|
+
responseInit.event_uuid && insertParam('event_uuid', responseInit.event_uuid);
|
59
|
+
insertParam('visitor_uuid', responseInit.visitor_uuid);
|
60
|
+
replaceUrl();
|
61
|
+
}
|
62
|
+
const responseStart = await startTracker(root);
|
63
|
+
if (responseStart) {
|
64
|
+
responseStart.event_uuid && insertParam('event_uuid_start', responseStart.event_uuid);
|
65
|
+
insertParam('visitor_uuid_start', responseStart.visitor_uuid);
|
66
|
+
replaceUrl();
|
67
|
+
}
|
68
|
+
}
|
69
|
+
};
|
70
|
+
|
71
|
+
document.addEventListener('readystatechange', update, true);
|
72
|
+
|
73
|
+
window.addEventListener('beforeunload', async () => {
|
74
|
+
await endTracker(root);
|
75
|
+
});
|
76
|
+
|
77
|
+
update();
|
78
|
+
};
|
79
|
+
const insertParam = (key, value) => {
|
80
|
+
const url = new URL(window.location.href);
|
81
|
+
url.searchParams.set(key, value);
|
82
|
+
window.history.pushState({ path: url.href }, '', url.href);
|
83
|
+
};
|
84
|
+
|
85
|
+
const replaceUrl = () => {
|
86
|
+
const urlParams = new URLSearchParams(window.location.search);
|
87
|
+
const event_id = urlParams.get('event_id');
|
88
|
+
const uuid = urlParams.get('uuid');
|
89
|
+
|
90
|
+
let anchors = document.getElementsByTagName('a');
|
91
|
+
for (let i = 0; i < anchors.length; i++) {
|
92
|
+
const eventIdParams = getParameterByName('event_id', anchors[i].href);
|
93
|
+
const uuidParams = getParameterByName('uuid', anchors[i].href);
|
94
|
+
if (anchors[i].href) {
|
95
|
+
const url = new URL(anchors[i].href);
|
96
|
+
!eventIdParams && event_id && url.searchParams.append('event_id', event_id);
|
97
|
+
!uuidParams && uuid && url.searchParams.append('uuid', uuid);
|
98
|
+
anchors[i].href = url.href;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
};
|
102
|
+
|
103
|
+
const getParameterByName = (name, url = window.location.href) => {
|
104
|
+
name = name.replace(/[[\]]/g, '\\$&');
|
105
|
+
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
|
106
|
+
results = regex.exec(url);
|
107
|
+
if (!results) return null;
|
108
|
+
if (!results[2]) return '';
|
109
|
+
return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
110
|
+
};
|
111
|
+
|
112
|
+
AesirAnalytics();
|
package/src/index.js
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
/*
|
2
|
+
* @copyright Copyright (C) 2022 AesirX. All rights reserved.
|
3
|
+
* @license GNU General Public License version 3, see LICENSE.
|
4
|
+
*/
|
5
|
+
|
6
|
+
import React from 'react';
|
7
|
+
export const AnalyticsContext = React.createContext();
|
8
|
+
|
9
|
+
export class AnalyticsContextProvider extends React.Component {
|
10
|
+
render() {
|
11
|
+
return (
|
12
|
+
<AnalyticsContext.Provider value={{ ...this.props.value }}>
|
13
|
+
{this.props.children}
|
14
|
+
</AnalyticsContext.Provider>
|
15
|
+
);
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
import axios from 'axios';
|
2
|
+
import { trackerService } from './services';
|
3
|
+
import Bowser from 'bowser';
|
4
|
+
|
5
|
+
const createRequest = (endpoint, task) => {
|
6
|
+
return `${endpoint}/visitor/v1/${task}`;
|
7
|
+
};
|
8
|
+
const getIpAddress = async () => {
|
9
|
+
const res = await axios.get('https://geolocation-db.com/json/');
|
10
|
+
return res.data.IPv4;
|
11
|
+
};
|
12
|
+
|
13
|
+
/* FUNCTION */
|
14
|
+
const initTracker = async (endpoint, url, referrer, user_agent) => {
|
15
|
+
const { document } = window;
|
16
|
+
const { pathname, search, origin } = location;
|
17
|
+
url = `${origin}${pathname}${search}`;
|
18
|
+
referrer = document.referrer;
|
19
|
+
user_agent = window.navigator.userAgent;
|
20
|
+
const browser = Bowser.parse(window.navigator.userAgent);
|
21
|
+
const browser_name = browser?.browser?.name;
|
22
|
+
const browser_version = browser?.browser?.version;
|
23
|
+
const lang = window.navigator.userLanguage || window.navigator.language;
|
24
|
+
const device = browser?.platform?.model ?? browser?.platform?.type;
|
25
|
+
const domain = `${origin}`;
|
26
|
+
const queryString = window.location.search;
|
27
|
+
const urlParams = new URLSearchParams(queryString);
|
28
|
+
if (!urlParams.get('event_id') && !urlParams.get('uuid')) {
|
29
|
+
let ip = await getIpAddress();
|
30
|
+
const response = await trackerService(createRequest(endpoint, 'init'), {
|
31
|
+
url: url,
|
32
|
+
referrer: referrer,
|
33
|
+
user_agent: user_agent,
|
34
|
+
ip: ip,
|
35
|
+
domain: domain,
|
36
|
+
browser_name: browser_name,
|
37
|
+
browser_version: browser_version,
|
38
|
+
lang: lang,
|
39
|
+
device: device,
|
40
|
+
});
|
41
|
+
return response;
|
42
|
+
}
|
43
|
+
};
|
44
|
+
|
45
|
+
const startTracker = async (endpoint, event_uuid, visitor_uuid, referrer) => {
|
46
|
+
const { location, document } = window;
|
47
|
+
referrer = referrer
|
48
|
+
? location.protocol + '//' + location.host + referrer
|
49
|
+
: document.referrer.split('?')[0];
|
50
|
+
const url = location.protocol + '//' + location.host + location.pathname;
|
51
|
+
const queryString = window.location.search;
|
52
|
+
const urlParams = new URLSearchParams(queryString);
|
53
|
+
const responseStart = await trackerService(createRequest(endpoint, 'start'), {
|
54
|
+
...(urlParams.get('event_uuid') && {
|
55
|
+
event_uuid: urlParams.get('event_uuid'),
|
56
|
+
}),
|
57
|
+
...(urlParams.get('visitor_uuid') && {
|
58
|
+
visitor_uuid: urlParams.get('visitor_uuid'),
|
59
|
+
}),
|
60
|
+
...(event_uuid && {
|
61
|
+
event_uuid: event_uuid,
|
62
|
+
}),
|
63
|
+
...(visitor_uuid && {
|
64
|
+
visitor_uuid: visitor_uuid,
|
65
|
+
}),
|
66
|
+
referrer: referrer === '/' ? '' : referrer,
|
67
|
+
url: url,
|
68
|
+
});
|
69
|
+
|
70
|
+
return responseStart;
|
71
|
+
};
|
72
|
+
|
73
|
+
const endTracker = async (endpoint, event_uuid, visitor_uuid) => {
|
74
|
+
const queryString = window.location.search;
|
75
|
+
const urlParams = new URLSearchParams(queryString);
|
76
|
+
const responseEnd = await trackerService(createRequest(endpoint, 'end'), {
|
77
|
+
...(urlParams.get('event_uuid_start') && {
|
78
|
+
event_uuid: urlParams.get('event_uuid_start'),
|
79
|
+
}),
|
80
|
+
...(urlParams.get('visitor_uuid_start') && {
|
81
|
+
visitor_uuid: urlParams.get('visitor_uuid_start'),
|
82
|
+
}),
|
83
|
+
...(event_uuid && {
|
84
|
+
event_uuid: event_uuid,
|
85
|
+
}),
|
86
|
+
...(visitor_uuid && {
|
87
|
+
visitor_uuid: visitor_uuid,
|
88
|
+
}),
|
89
|
+
});
|
90
|
+
return responseEnd;
|
91
|
+
};
|
92
|
+
|
93
|
+
export { initTracker, startTracker, endTracker };
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/* SERVICES */
|
2
|
+
let cache;
|
3
|
+
const assign = (a, b) => {
|
4
|
+
Object.keys(b).forEach((key) => {
|
5
|
+
if (b[key] !== undefined) a[key] = b[key];
|
6
|
+
});
|
7
|
+
return a;
|
8
|
+
};
|
9
|
+
|
10
|
+
const trackerService = async (endpoint, payload) => {
|
11
|
+
const fetchData = await fetch(endpoint, {
|
12
|
+
method: 'POST',
|
13
|
+
body: JSON.stringify(payload),
|
14
|
+
headers: assign({ 'Content-Type': 'application/json' }, { ['x-tracker-cache']: cache }),
|
15
|
+
});
|
16
|
+
const response = await fetchData.json();
|
17
|
+
return response;
|
18
|
+
};
|
19
|
+
|
20
|
+
export { trackerService };
|
package/tsconfig.json
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"listEmittedFiles": false,
|
4
|
+
"outDir": "dist/lib/types",
|
5
|
+
"declaration": true,
|
6
|
+
"declarationMap": true,
|
7
|
+
"module": "ESNext",
|
8
|
+
"target": "ES6",
|
9
|
+
"noImplicitAny": true,
|
10
|
+
"noImplicitThis": true,
|
11
|
+
"removeComments": false,
|
12
|
+
"suppressImplicitAnyIndexErrors": true,
|
13
|
+
"sourceMap": true,
|
14
|
+
"pretty": true,
|
15
|
+
"allowJs": true,
|
16
|
+
"jsx": "react",
|
17
|
+
"lib": [
|
18
|
+
"es2016",
|
19
|
+
"dom"
|
20
|
+
]
|
21
|
+
},
|
22
|
+
"include": [
|
23
|
+
"src/**/*"
|
24
|
+
],
|
25
|
+
"exclude": [
|
26
|
+
"./node_modules/**/*",
|
27
|
+
"./dist/**/*",
|
28
|
+
"./src/analytics.js",
|
29
|
+
],
|
30
|
+
}
|