@armco/analytics 0.1.5 → 0.1.7
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/index.js +13 -9
- package/package.json +4 -26
- package/dist/analytics.d.ts +0 -19
- package/dist/analytics.js +0 -456
- package/dist/constants.d.ts +0 -3
- package/dist/constants.js +0 -3
- package/dist/flush.d.ts +0 -3
- package/dist/flush.js +0 -36
- package/dist/helper.d.ts +0 -1
- package/dist/helper.js +0 -3
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -13
- package/dist/location.d.ts +0 -6
- package/dist/location.js +0 -40
- package/dist/session.d.ts +0 -3
- package/dist/session.js +0 -51
- package/global-modules.d.ts +0 -14
- package/index.d.ts +0 -34
package/index.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, } from "./analytics";
|
|
2
|
+
document.addEventListener("DOMContentLoaded", function (event) {
|
|
3
|
+
const environment = getEnvironment();
|
|
4
|
+
console.info("[ANALYTICS] Idenfitied environment: ", environment);
|
|
5
|
+
if (!environment || environment === "development") {
|
|
6
|
+
console.info("[ANALYTICS] Attempting to auto-load analytics configurations from environment...");
|
|
7
|
+
init();
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
console.info("[ANALYTICS] Identified non-dev environment, analytics auto load wouldn't be attempted.");
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
export { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
|
package/package.json
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armco/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Browser Based Analytics interceptor for configured events",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "npx ts-node build.js",
|
|
9
|
-
"lint": "npx eslint --ext .ts src/",
|
|
10
|
-
"lint:tests": "npx eslint --ext .ts spec/",
|
|
11
|
-
"start": "node ./dist --env=production",
|
|
12
|
-
"dev": "nodemon",
|
|
13
|
-
"test": "nodemon --config ./spec/nodemon.json",
|
|
14
|
-
"test:no-reloading": "npx ts-node --files -r tsconfig-paths/register ./spec",
|
|
15
|
-
"publish:local": "npm run build && npm pack --pack-destination ~/__Projects__/Common",
|
|
16
|
-
"publish:sh": "./publish.sh",
|
|
17
|
-
"publish:sh:minor": "./publish.sh minor"
|
|
18
|
-
},
|
|
7
|
+
"scripts": {},
|
|
19
8
|
"repository": {
|
|
20
9
|
"type": "git",
|
|
21
10
|
"url": "git+https://github.com/ReStruct-Corporate-Advantage/analytics.git"
|
|
@@ -34,20 +23,9 @@
|
|
|
34
23
|
},
|
|
35
24
|
"homepage": "https://github.com/ReStruct-Corporate-Advantage/analytics#readme",
|
|
36
25
|
"files": [
|
|
37
|
-
"
|
|
38
|
-
"./index.js",
|
|
39
|
-
"./index.d.ts",
|
|
40
|
-
"./global-modules.d.ts"
|
|
26
|
+
"."
|
|
41
27
|
],
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@types/fs-extra": "^11.0.1",
|
|
44
|
-
"@types/jquery": "^3.5.16",
|
|
45
|
-
"@types/js-cookie": "^3.0.3",
|
|
46
|
-
"@types/node": "^20.4.2",
|
|
47
|
-
"@types/uuid": "^9.0.2",
|
|
48
|
-
"fs-extra": "^11.1.1",
|
|
49
|
-
"typescript": "^5.1.6"
|
|
50
|
-
},
|
|
28
|
+
"devDependencies": {},
|
|
51
29
|
"dependencies": {
|
|
52
30
|
"jet-logger": "^1.3.1",
|
|
53
31
|
"jquery": "^3.7.0",
|
package/dist/analytics.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { startSession, getSessionId } from "./session";
|
|
2
|
-
import { User, Event, ConfigType } from "./index.d";
|
|
3
|
-
declare let enabled: boolean | null;
|
|
4
|
-
declare function loadConfiguration(): Promise<any>;
|
|
5
|
-
declare function getEnvironment(): string;
|
|
6
|
-
declare function getEnvironmentType(): 'browser' | 'node' | 'unknown';
|
|
7
|
-
export declare function sendBulkData(data: Event[], callback?: Function): Promise<void>;
|
|
8
|
-
declare function trackEvent(event: string | Event, data?: any): void;
|
|
9
|
-
declare function trackPageView(pageName: string, data?: {
|
|
10
|
-
[key: string]: any;
|
|
11
|
-
}): void;
|
|
12
|
-
declare function trackError(errorMessage: string): void;
|
|
13
|
-
declare function enableTracking(enable: boolean): void;
|
|
14
|
-
declare function generateAnonymousId(): string;
|
|
15
|
-
declare function identify(appUser: User): void;
|
|
16
|
-
declare function sendHostProjectName(): void;
|
|
17
|
-
declare function init(config?: ConfigType): void;
|
|
18
|
-
declare function logout(): void;
|
|
19
|
-
export { enabled as analyticsEnabled, init, identify, getEnvironmentType, getEnvironment, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
|
package/dist/analytics.js
DELETED
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import * as fs from 'fs';
|
|
11
|
-
import * as path from 'path';
|
|
12
|
-
import $ from "jquery";
|
|
13
|
-
import { v4 as uuidv4 } from "uuid";
|
|
14
|
-
import { ADD, ARMCO_SERVER } from "./constants";
|
|
15
|
-
import { startSession, getSessionId, terminateSession } from "./session";
|
|
16
|
-
import { ipLookup, success, error, localTimeRegion } from "./location";
|
|
17
|
-
import { isArClient } from './helper';
|
|
18
|
-
import { hookFlushHandlers, queueEvent } from './flush';
|
|
19
|
-
const tsConfigPath = getEnvironmentType() === "node" ? path.resolve(process.cwd(), 'tsconfig.json') : "../../../../tsConfig.json";
|
|
20
|
-
const packageJsonPath = getEnvironmentType() === "node" ? path.resolve(process.cwd(), 'package.json') : "../../../../package.json";
|
|
21
|
-
let ar_anonymous_id;
|
|
22
|
-
let user = null;
|
|
23
|
-
let apiKey = null;
|
|
24
|
-
let analyticsEndpoint;
|
|
25
|
-
let hostProjectName = null;
|
|
26
|
-
let enabled = null;
|
|
27
|
-
const CONFIG_FILE_NAME = "analyticsrc";
|
|
28
|
-
let region;
|
|
29
|
-
let address;
|
|
30
|
-
let coordinates;
|
|
31
|
-
let runtime = null;
|
|
32
|
-
let CONFIG;
|
|
33
|
-
let environment;
|
|
34
|
-
function isTypeScriptProject() {
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
if (fs && "existsSync" in fs && !(fs.existsSync(tsConfigPath) && fs.existsSync(packageJsonPath))) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
let tsConfig, packageJson;
|
|
40
|
-
try {
|
|
41
|
-
tsConfig = yield import(tsConfigPath);
|
|
42
|
-
if (tsConfig && tsConfig.compilerOptions) {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
console.error('Error loading tsconfig.json:', error);
|
|
48
|
-
}
|
|
49
|
-
try {
|
|
50
|
-
packageJson = yield import(tsConfigPath);
|
|
51
|
-
if (packageJson &&
|
|
52
|
-
packageJson.devDependencies &&
|
|
53
|
-
packageJson.devDependencies.typescript) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
console.error('Error loading tsconfig.json:', error);
|
|
59
|
-
}
|
|
60
|
-
if (packageJson &&
|
|
61
|
-
packageJson.devDependencies &&
|
|
62
|
-
packageJson.devDependencies.typescript) {
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
function loadConfiguration() {
|
|
69
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
const ROOT = (runtime || getEnvironment()) === "production" ? "../../" : "../../../../";
|
|
71
|
-
let configFilePath = `${ROOT}${CONFIG_FILE_NAME}.json`;
|
|
72
|
-
try {
|
|
73
|
-
const config = yield import(configFilePath);
|
|
74
|
-
return config;
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
78
|
-
}
|
|
79
|
-
const isTS = yield isTypeScriptProject();
|
|
80
|
-
configFilePath = `${ROOT}${CONFIG_FILE_NAME}.${isTS ? "ts" : "js"}`;
|
|
81
|
-
try {
|
|
82
|
-
const config = yield import(configFilePath);
|
|
83
|
-
console.log(`Configuration loaded from ${configFilePath} successfully.`);
|
|
84
|
-
return config.default;
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
88
|
-
}
|
|
89
|
-
console.error(`No valid configuration file found. Expected one of ${CONFIG_FILE_NAME}.js, ${CONFIG_FILE_NAME}.json or ${CONFIG_FILE_NAME}.ts`);
|
|
90
|
-
return null;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
function getEnvironment() {
|
|
94
|
-
if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV) {
|
|
95
|
-
return process.env.NODE_ENV;
|
|
96
|
-
}
|
|
97
|
-
if (import.meta.env && import.meta.env.MODE) {
|
|
98
|
-
return import.meta.env.MODE;
|
|
99
|
-
}
|
|
100
|
-
return 'development';
|
|
101
|
-
}
|
|
102
|
-
function getEnvironmentType() {
|
|
103
|
-
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
|
|
104
|
-
return 'browser';
|
|
105
|
-
}
|
|
106
|
-
else if (typeof process !== 'undefined' &&
|
|
107
|
-
process.versions != null &&
|
|
108
|
-
process.versions.node != null) {
|
|
109
|
-
return 'node';
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
return 'unknown';
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
function getHostProjectName() {
|
|
116
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
if (!hostProjectName) {
|
|
118
|
-
const packageJson = yield import(packageJsonPath);
|
|
119
|
-
hostProjectName = packageJson.name || null;
|
|
120
|
-
enabled = isEnabled();
|
|
121
|
-
}
|
|
122
|
-
return hostProjectName;
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
function enrichEventData(data) {
|
|
126
|
-
if (data) {
|
|
127
|
-
data.eventId = uuidv4();
|
|
128
|
-
data.client = hostProjectName;
|
|
129
|
-
data.sessionId = getSessionId();
|
|
130
|
-
region && (data.region = region);
|
|
131
|
-
address && !data.address && (data.address = address);
|
|
132
|
-
coordinates && !data.coordinates && (data.coordinates = coordinates);
|
|
133
|
-
!data.timestamp && (data.timestamp = new Date());
|
|
134
|
-
!data.userId && (data.userId = user ? user.email : ar_anonymous_id);
|
|
135
|
-
!data.email && user && user.email && (data.email = user.email);
|
|
136
|
-
user && (data.user = user);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
function isMalformedEvent(data) {
|
|
140
|
-
return !data || !data.eventType;
|
|
141
|
-
}
|
|
142
|
-
export function sendBulkData(data, callback) {
|
|
143
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
-
const promises = data && data.map((event) => sendAnalyticsData(event));
|
|
145
|
-
Promise.all(promises).then(() => callback && callback());
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
function sendAnalyticsData(data) {
|
|
149
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
-
try {
|
|
151
|
-
if (!apiKey && !analyticsEndpoint) {
|
|
152
|
-
console.error('Neither of API key and Analytics server configured. Data not sent.');
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
const options = {
|
|
156
|
-
method: "POST",
|
|
157
|
-
headers: {
|
|
158
|
-
'Content-Type': 'application/json',
|
|
159
|
-
},
|
|
160
|
-
body: JSON.stringify({ event: data }),
|
|
161
|
-
};
|
|
162
|
-
if (apiKey) {
|
|
163
|
-
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
164
|
-
}
|
|
165
|
-
const logEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsEndpoint;
|
|
166
|
-
const response = yield fetch(logEndpoint, options);
|
|
167
|
-
console.log('Analytics data sent to server:', logEndpoint, data, response.status, response.statusText);
|
|
168
|
-
}
|
|
169
|
-
catch (error) {
|
|
170
|
-
console.error('Failed to send analytics data:', error);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function tagEvents(email) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
try {
|
|
177
|
-
if (!apiKey && !analyticsEndpoint) {
|
|
178
|
-
console.error('Neither of API key and Analytics server configured. Tagging won\'t be attempted.');
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
const options = {
|
|
182
|
-
method: "POST",
|
|
183
|
-
headers: {
|
|
184
|
-
'Content-Type': 'application/json',
|
|
185
|
-
},
|
|
186
|
-
body: { event: JSON.stringify({ email, anonymousId: ar_anonymous_id }) },
|
|
187
|
-
};
|
|
188
|
-
if (apiKey) {
|
|
189
|
-
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
190
|
-
}
|
|
191
|
-
const tagEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsEndpoint;
|
|
192
|
-
const response = yield fetch(tagEndpoint, options);
|
|
193
|
-
console.log('Analytics data sent to server:', tagEndpoint, response.status, response.statusText);
|
|
194
|
-
}
|
|
195
|
-
catch (error) {
|
|
196
|
-
console.error('Failed to send analytics data:', error);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
function trackEvent(event, data) {
|
|
201
|
-
const tracker = user && user.email ? user.email : ar_anonymous_id;
|
|
202
|
-
if (enabled && tracker) {
|
|
203
|
-
if (typeof event === "string") {
|
|
204
|
-
data = data || {};
|
|
205
|
-
if (data) {
|
|
206
|
-
data.eventType = event;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
data = event;
|
|
211
|
-
}
|
|
212
|
-
if (isMalformedEvent(data)) {
|
|
213
|
-
console.error("Attempting to send empty event, or event missing eventType, failing send...");
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
enrichEventData(data);
|
|
217
|
-
(CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.submissionStrategy) === "DEFER" ? queueEvent(data) : sendAnalyticsData(data);
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
console.log('Analytics disabled or user not identified. Event data not sent.');
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
function trackPageView(pageName, data) {
|
|
224
|
-
const pageViewEvent = Object.assign({ eventType: "Page View", pageName }, data);
|
|
225
|
-
trackEvent('Page View', pageViewEvent);
|
|
226
|
-
}
|
|
227
|
-
function trackError(errorMessage) {
|
|
228
|
-
const errorEvent = {
|
|
229
|
-
eventType: 'Error',
|
|
230
|
-
timestamp: new Date(),
|
|
231
|
-
errorMessage,
|
|
232
|
-
};
|
|
233
|
-
trackEvent(errorEvent);
|
|
234
|
-
}
|
|
235
|
-
function isEnabled() {
|
|
236
|
-
if (typeof navigator !== 'undefined' && 'doNotTrack' in navigator) {
|
|
237
|
-
const doNotTrackValue = navigator.doNotTrack;
|
|
238
|
-
if (doNotTrackValue === '1' || doNotTrackValue === 'yes') {
|
|
239
|
-
console.warn("[ANALYTICS] Tracking disabled in client, events will not be logged!");
|
|
240
|
-
return isArClient(hostProjectName) || false;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return true;
|
|
244
|
-
}
|
|
245
|
-
function enableTracking(enable) {
|
|
246
|
-
enabled = isEnabled() && enable;
|
|
247
|
-
}
|
|
248
|
-
function generateAnonymousId() {
|
|
249
|
-
ar_anonymous_id = uuidv4();
|
|
250
|
-
return ar_anonymous_id;
|
|
251
|
-
}
|
|
252
|
-
function populateLocationDetails() {
|
|
253
|
-
if (!navigator.geolocation) {
|
|
254
|
-
console.warn("Geolocation is not supported by your browser");
|
|
255
|
-
ipLookup();
|
|
256
|
-
}
|
|
257
|
-
else {
|
|
258
|
-
navigator.geolocation.getCurrentPosition((position) => {
|
|
259
|
-
coordinates = { latitude: position.coords.latitude, longitude: position.coords.longitude };
|
|
260
|
-
success(position, (reverseGeocodingResponse) => {
|
|
261
|
-
address = reverseGeocodingResponse.results[0].formatted_address;
|
|
262
|
-
});
|
|
263
|
-
}, error);
|
|
264
|
-
}
|
|
265
|
-
region = localTimeRegion;
|
|
266
|
-
}
|
|
267
|
-
function hookTrackers() {
|
|
268
|
-
if (environment === "browser") {
|
|
269
|
-
const TRACK_EVENTS = (CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.trackEvents) || ["click", "submit", "select-change"];
|
|
270
|
-
if (TRACK_EVENTS.indexOf("click") > -1) {
|
|
271
|
-
console.log("[ANALYTICS] Attaching Click handlers");
|
|
272
|
-
const clickables = Array.from(document.querySelectorAll('*'));
|
|
273
|
-
console.log("[ANALYTICS] Found " + clickables.length + " items that can be clicked!");
|
|
274
|
-
clickables
|
|
275
|
-
.filter((element) => {
|
|
276
|
-
return element.tagName === "BUTTON" ||
|
|
277
|
-
element.tagName === "A" ||
|
|
278
|
-
element.onclick != null ||
|
|
279
|
-
window.getComputedStyle(element).cursor == "pointer";
|
|
280
|
-
})
|
|
281
|
-
.forEach((element) => {
|
|
282
|
-
element.addEventListener("click", e => {
|
|
283
|
-
var _a;
|
|
284
|
-
const clickedElement = e.target;
|
|
285
|
-
const id = clickedElement.id;
|
|
286
|
-
const classes = Array.from(clickedElement.classList);
|
|
287
|
-
const dataAttributes = Object.keys(clickedElement.dataset).map(key => ({ [key]: clickedElement.dataset[key] }));
|
|
288
|
-
const textContent = clickedElement.textContent;
|
|
289
|
-
const href = 'href' in clickedElement ? clickedElement.href : null;
|
|
290
|
-
const elementType = clickedElement.tagName.toLowerCase();
|
|
291
|
-
const parentElementId = ((_a = clickedElement.parentElement) === null || _a === void 0 ? void 0 : _a.id) || null;
|
|
292
|
-
const value = 'value' in clickedElement && clickedElement.value
|
|
293
|
-
? clickedElement.value
|
|
294
|
-
: null;
|
|
295
|
-
trackEvent("CLICK", {
|
|
296
|
-
click: {
|
|
297
|
-
id,
|
|
298
|
-
classes,
|
|
299
|
-
dataAttributes,
|
|
300
|
-
textContent,
|
|
301
|
-
href,
|
|
302
|
-
elementType,
|
|
303
|
-
parentElementId,
|
|
304
|
-
value,
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
console.log("[ANALYTICS] Click handlers Attached");
|
|
310
|
-
}
|
|
311
|
-
if (TRACK_EVENTS.indexOf("submit") > -1) {
|
|
312
|
-
console.log("[ANALYTICS] Attaching Submit handlers");
|
|
313
|
-
document.addEventListener('submit', event => {
|
|
314
|
-
const formElement = event.target;
|
|
315
|
-
trackEvent('SUBMIT', {
|
|
316
|
-
submit: {
|
|
317
|
-
formId: formElement.id,
|
|
318
|
-
formData: new FormData(formElement)
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
console.log("[ANALYTICS] Submit handlers attached");
|
|
323
|
-
}
|
|
324
|
-
if (TRACK_EVENTS.indexOf("select-change") > -1) {
|
|
325
|
-
console.log("[ANALYTICS] Attaching Select OnChange handlers");
|
|
326
|
-
document.addEventListener('change', (event) => {
|
|
327
|
-
const target = event.target;
|
|
328
|
-
if (target.tagName === 'SELECT') {
|
|
329
|
-
const selectedOptionValue = target.value;
|
|
330
|
-
const selectedOptionLabel = target.selectedOptions[0].label;
|
|
331
|
-
trackEvent('SELECT_CHANGE', { value: selectedOptionValue, label: selectedOptionLabel });
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
console.log("[ANALYTICS] Select OnChange handlers attached");
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
function identify(appUser) {
|
|
339
|
-
if (user === null) {
|
|
340
|
-
user = appUser;
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
user.email = appUser.email;
|
|
344
|
-
}
|
|
345
|
-
tagEvents(user.email);
|
|
346
|
-
ar_anonymous_id = null;
|
|
347
|
-
}
|
|
348
|
-
function sendHostProjectName() {
|
|
349
|
-
if (hostProjectName) {
|
|
350
|
-
const data = {
|
|
351
|
-
hostProjectName,
|
|
352
|
-
};
|
|
353
|
-
sendAnalyticsData(data);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
function showTrackingPopup() {
|
|
357
|
-
const popupContent = `
|
|
358
|
-
<div class="tracking-popup" style="position: fixed;
|
|
359
|
-
width: 50%;
|
|
360
|
-
left: 25%;
|
|
361
|
-
bottom: 2rem;
|
|
362
|
-
padding: 1rem;
|
|
363
|
-
background: aliceblue;
|
|
364
|
-
border-radius: 5px;
|
|
365
|
-
display: flex;
|
|
366
|
-
border: 1px solid #ccc;
|
|
367
|
-
justify-content: space-between;">
|
|
368
|
-
<p style="width: 80%;">We use cookies to collect and analyze data to improve our website. By clicking "Accept," you consent to the use of cookies.</p>
|
|
369
|
-
<a class="btn-accept" style="display: flex;
|
|
370
|
-
align-items: center;
|
|
371
|
-
font-weight: bold;
|
|
372
|
-
cursor: pointer;"
|
|
373
|
-
onClick="${(e) => {
|
|
374
|
-
e.preventDefault();
|
|
375
|
-
enableTracking(true);
|
|
376
|
-
}};"
|
|
377
|
-
>Got it!</a>
|
|
378
|
-
</div>
|
|
379
|
-
`;
|
|
380
|
-
$('body').append(popupContent);
|
|
381
|
-
$('.btn-accept').on('click', function () {
|
|
382
|
-
$('.tracking-popup').remove();
|
|
383
|
-
});
|
|
384
|
-
$('.btn-decline').on('click', function () {
|
|
385
|
-
enableTracking(false);
|
|
386
|
-
$('.tracking-popup').remove();
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
function loadAnalytics(config) {
|
|
390
|
-
console.log("[ANALYTICS] Loading Configuration");
|
|
391
|
-
CONFIG = config;
|
|
392
|
-
if (CONFIG) {
|
|
393
|
-
console.log("[ANALYTICS] Configuration loaded");
|
|
394
|
-
apiKey = CONFIG.apiKey || null;
|
|
395
|
-
analyticsEndpoint = CONFIG.analyticsEndpoint || null;
|
|
396
|
-
hostProjectName = CONFIG.hostProjectName || hostProjectName || null;
|
|
397
|
-
console.log("[ANALYTICS] Check if Analytics enabled");
|
|
398
|
-
enabled = isEnabled();
|
|
399
|
-
if (enabled) {
|
|
400
|
-
console.log("[ANALYTICS] Display Tracker Popup");
|
|
401
|
-
CONFIG.showPopUp && showTrackingPopup();
|
|
402
|
-
console.log("[ANALYTICS] Hook Event Trackers");
|
|
403
|
-
hookTrackers();
|
|
404
|
-
console.log("[ANALYTICS] Hook Handlers to flush events (use when submissionStrategy is configured as \"DEFER\"");
|
|
405
|
-
hookFlushHandlers(CONFIG.submissionStrategy);
|
|
406
|
-
console.log("[ANALYTICS] Find User Location Details");
|
|
407
|
-
populateLocationDetails();
|
|
408
|
-
console.log("[ANALYTICS] Initiate Session and Anonymous User ID");
|
|
409
|
-
const anonId = generateAnonymousId();
|
|
410
|
-
console.log("Tracking User as", anonId);
|
|
411
|
-
startSession();
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
console.warn("[ANALYTICS] Analytics blocked by client, or disabled by configuration!");
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
function init(config) {
|
|
419
|
-
try {
|
|
420
|
-
environment = getEnvironmentType();
|
|
421
|
-
runtime = getEnvironment();
|
|
422
|
-
const projectName = config && config.hostProjectName;
|
|
423
|
-
if (projectName) {
|
|
424
|
-
hostProjectName = projectName;
|
|
425
|
-
}
|
|
426
|
-
else {
|
|
427
|
-
getHostProjectName()
|
|
428
|
-
.then((projectName) => {
|
|
429
|
-
hostProjectName = projectName;
|
|
430
|
-
if (config) {
|
|
431
|
-
loadAnalytics(config);
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
loadConfiguration()
|
|
435
|
-
.then(config => {
|
|
436
|
-
loadAnalytics(config);
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
})
|
|
440
|
-
.catch((err) => {
|
|
441
|
-
console.error("[ANALYTICS] Couldn't determine host project name, no events will be logged!");
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
catch (e) {
|
|
446
|
-
console.log("[ANALYTICS]", e);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
function logout() {
|
|
450
|
-
if (user) {
|
|
451
|
-
user = null;
|
|
452
|
-
generateAnonymousId();
|
|
453
|
-
}
|
|
454
|
-
terminateSession();
|
|
455
|
-
}
|
|
456
|
-
export { enabled as analyticsEnabled, init, identify, getEnvironmentType, getEnvironment, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
|
package/dist/constants.d.ts
DELETED
package/dist/constants.js
DELETED
package/dist/flush.d.ts
DELETED
package/dist/flush.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { sendBulkData } from "./analytics";
|
|
2
|
-
const MAX_EVENTS = 100;
|
|
3
|
-
const FLUSH_INTERVAL = 15000;
|
|
4
|
-
let events = [];
|
|
5
|
-
export function queueEvent(event) {
|
|
6
|
-
events.push(event);
|
|
7
|
-
if (events.length >= MAX_EVENTS) {
|
|
8
|
-
flushEvents();
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
function flushEvents() {
|
|
12
|
-
sendBulkData(events, () => {
|
|
13
|
-
events = [];
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
export function hookFlushHandlers(submissionStrategy = "ONEVENT") {
|
|
17
|
-
if (typeof window !== 'undefined' && submissionStrategy === "DEFER") {
|
|
18
|
-
function handleBeforeUnload() {
|
|
19
|
-
if (events.length > 0) {
|
|
20
|
-
flushEvents();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function handleVisibilityChange() {
|
|
24
|
-
if (document.visibilityState === 'hidden' && events.length > 0) {
|
|
25
|
-
flushEvents();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
29
|
-
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
30
|
-
setInterval(() => {
|
|
31
|
-
if (events.length > 0) {
|
|
32
|
-
flushEvents();
|
|
33
|
-
}
|
|
34
|
-
}, FLUSH_INTERVAL);
|
|
35
|
-
}
|
|
36
|
-
}
|
package/dist/helper.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isArClient(name: string | null): boolean;
|
package/dist/helper.js
DELETED
package/dist/index.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession } from "./analytics";
|
|
2
|
-
export { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
|
package/dist/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, } from "./analytics";
|
|
2
|
-
document.addEventListener("DOMContentLoaded", function (event) {
|
|
3
|
-
const environment = getEnvironment();
|
|
4
|
-
console.info("[ANALYTICS] Idenfitied environment: ", environment);
|
|
5
|
-
if (!environment || environment === "development") {
|
|
6
|
-
console.info("[ANALYTICS] Attempting to auto-load analytics configurations from environment...");
|
|
7
|
-
init();
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
console.info("[ANALYTICS] Identified non-dev environment, analytics auto load wouldn't be attempted.");
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
export { init, identify, getEnvironment, getEnvironmentType, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
|
package/dist/location.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function ipLookup(): void;
|
|
2
|
-
export declare function success(position: any, callback: Function): void;
|
|
3
|
-
export declare function error(): void;
|
|
4
|
-
declare const localTimeRegion: string;
|
|
5
|
-
declare const localTime: string;
|
|
6
|
-
export { localTimeRegion, localTime };
|
package/dist/location.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import jstz from "jstz";
|
|
2
|
-
export function ipLookup() {
|
|
3
|
-
fetch('https://extreme-ip-lookup.com/json/')
|
|
4
|
-
.then(res => res.json())
|
|
5
|
-
.then(response => {
|
|
6
|
-
fallbackProcess(response);
|
|
7
|
-
})
|
|
8
|
-
.catch(() => {
|
|
9
|
-
console.log('We could not find your location');
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
export function success(position, callback) {
|
|
13
|
-
const latitude = position.coords.latitude;
|
|
14
|
-
const longitude = position.coords.longitude;
|
|
15
|
-
reverseGeocodingWithGoogle(latitude, longitude, callback);
|
|
16
|
-
}
|
|
17
|
-
export function error() {
|
|
18
|
-
console.log("Unable to retrieve your location");
|
|
19
|
-
}
|
|
20
|
-
function reverseGeocodingWithGoogle(latitude, longitude, callback) {
|
|
21
|
-
fetch(`https://maps.googleapis.com/maps/api/geocode/json?
|
|
22
|
-
latlng=${latitude},${longitude}&key={GOOGLE_MAP_KEY}`)
|
|
23
|
-
.then(res => res.json())
|
|
24
|
-
.then(response => {
|
|
25
|
-
callback ? callback(response) : processUserData(response);
|
|
26
|
-
})
|
|
27
|
-
.catch(status => {
|
|
28
|
-
ipLookup();
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
function processUserData(response) {
|
|
32
|
-
console.log(response.results[0].formatted_address);
|
|
33
|
-
}
|
|
34
|
-
function fallbackProcess(response) {
|
|
35
|
-
const address = document.querySelector('.address');
|
|
36
|
-
address.innerText = `${response.city}, ${response.country}`;
|
|
37
|
-
}
|
|
38
|
-
const localTimeRegion = jstz.determine().name();
|
|
39
|
-
const localTime = new Date().toLocaleString("en-US", { timeZone: localTimeRegion });
|
|
40
|
-
export { localTimeRegion, localTime };
|
package/dist/session.d.ts
DELETED
package/dist/session.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import Cookies from 'js-cookie';
|
|
2
|
-
import { v4 as uuidv4 } from "uuid";
|
|
3
|
-
const SESSION_COOKIE_NAME = 'ar_session_id';
|
|
4
|
-
const SESSION_EXPIRATION_TIME = 30;
|
|
5
|
-
let localStorageTimeout;
|
|
6
|
-
function generateSessionId() {
|
|
7
|
-
return uuidv4();
|
|
8
|
-
}
|
|
9
|
-
export function startSession() {
|
|
10
|
-
const sessionId = generateSessionId();
|
|
11
|
-
const expirationDate = new Date();
|
|
12
|
-
expirationDate.setMinutes(expirationDate.getMinutes() + SESSION_EXPIRATION_TIME);
|
|
13
|
-
try {
|
|
14
|
-
Cookies.set(SESSION_COOKIE_NAME, sessionId, { expires: expirationDate });
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
localStorage.setItem(SESSION_COOKIE_NAME, sessionId);
|
|
18
|
-
localStorageTimeout = setTimeout(() => localStorage.removeItem(SESSION_COOKIE_NAME), SESSION_EXPIRATION_TIME * 1000);
|
|
19
|
-
}
|
|
20
|
-
return sessionId;
|
|
21
|
-
}
|
|
22
|
-
export function getSessionId() {
|
|
23
|
-
let sessionId;
|
|
24
|
-
sessionId = Cookies.get(SESSION_COOKIE_NAME);
|
|
25
|
-
if (!sessionId) {
|
|
26
|
-
sessionId = localStorage.getItem(SESSION_COOKIE_NAME);
|
|
27
|
-
}
|
|
28
|
-
if (!sessionId) {
|
|
29
|
-
return startSession();
|
|
30
|
-
}
|
|
31
|
-
if (sessionId) {
|
|
32
|
-
const expirationDate = new Date();
|
|
33
|
-
expirationDate.setMinutes(expirationDate.getMinutes() + SESSION_EXPIRATION_TIME);
|
|
34
|
-
try {
|
|
35
|
-
Cookies.set(SESSION_COOKIE_NAME, sessionId, { expires: expirationDate });
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
clearTimeout(localStorageTimeout);
|
|
39
|
-
localStorageTimeout = setTimeout(() => localStorage.removeItem(SESSION_COOKIE_NAME), SESSION_EXPIRATION_TIME * 1000);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return sessionId;
|
|
43
|
-
}
|
|
44
|
-
export function terminateSession() {
|
|
45
|
-
if (typeof window !== 'undefined' && window.Cookies) {
|
|
46
|
-
Cookies.remove(SESSION_COOKIE_NAME);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
localStorage.removeItem(SESSION_COOKIE_NAME);
|
|
50
|
-
}
|
|
51
|
-
}
|
package/global-modules.d.ts
DELETED
package/index.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import main from "./dist";
|
|
2
|
-
|
|
3
|
-
export interface User {
|
|
4
|
-
email: string;
|
|
5
|
-
// Other user properties...
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Event {
|
|
9
|
-
eventType: string;
|
|
10
|
-
timestamp?: Date;
|
|
11
|
-
region?: string,
|
|
12
|
-
address?: string,
|
|
13
|
-
coordinates?: {
|
|
14
|
-
latitude: number
|
|
15
|
-
longitude: number
|
|
16
|
-
}
|
|
17
|
-
[key: string]: any; // Index signature to accept any other properties with their associated types
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export interface ConfigType {
|
|
22
|
-
apiKey?: string;
|
|
23
|
-
analyticsEndpoint?: string;
|
|
24
|
-
hostProjectName?: string;
|
|
25
|
-
trackEvents?: Array<string>;
|
|
26
|
-
submissionStrategy?: SubmissionStrategy
|
|
27
|
-
showPopUp?: boolean
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type SubmissionStrategy = "ONEVENT" | "DEFER"
|
|
31
|
-
export type objType = { [key: string]: any }
|
|
32
|
-
export type EVENT_TYPES = "CLICK" | "SUBMIT" | "SCROLL" | string
|
|
33
|
-
|
|
34
|
-
export default main;
|