@armco/analytics 0.2.3 → 0.2.4
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/analytics.js +26 -13
- package/constants.d.ts +1 -1
- package/constants.js +1 -1
- package/index.interface.d.ts +2 -1
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -21,7 +21,8 @@ const packageJsonPath = getEnvironmentType() === "node" ? path.resolve(process.c
|
|
|
21
21
|
let ar_anonymous_id;
|
|
22
22
|
let user = null;
|
|
23
23
|
let apiKey = null;
|
|
24
|
-
let
|
|
24
|
+
let analyticsLogEndpoint;
|
|
25
|
+
let analyticsTagEndpoint;
|
|
25
26
|
let hostProjectName = null;
|
|
26
27
|
let enabled = null;
|
|
27
28
|
const CONFIG_FILE_NAME = "analyticsrc";
|
|
@@ -74,19 +75,19 @@ function loadConfiguration() {
|
|
|
74
75
|
return config;
|
|
75
76
|
}
|
|
76
77
|
catch (error) {
|
|
77
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
78
|
+
console.error(`[ANALYTICS] Failed to load configuration from ${configFilePath}.`);
|
|
78
79
|
}
|
|
79
80
|
const isTS = yield isTypeScriptProject();
|
|
80
81
|
configFilePath = `${ROOT}${CONFIG_FILE_NAME}.${isTS ? "ts" : "js"}`;
|
|
81
82
|
try {
|
|
82
83
|
const config = yield import(configFilePath);
|
|
83
|
-
console.log(`Configuration loaded from ${configFilePath} successfully.`);
|
|
84
|
+
console.log(`[ANALYTICS] Configuration loaded from ${configFilePath} successfully.`);
|
|
84
85
|
return config.default;
|
|
85
86
|
}
|
|
86
87
|
catch (error) {
|
|
87
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
88
|
+
console.error(`[ANALYTICS] Failed to load configuration from ${configFilePath}.`);
|
|
88
89
|
}
|
|
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
|
+
console.error(`[ANALYTICS] No valid configuration file found. Expected one of ${CONFIG_FILE_NAME}.js, ${CONFIG_FILE_NAME}.json or ${CONFIG_FILE_NAME}.ts`);
|
|
90
91
|
return null;
|
|
91
92
|
});
|
|
92
93
|
}
|
|
@@ -115,9 +116,15 @@ function getEnvironmentType() {
|
|
|
115
116
|
function getHostProjectName() {
|
|
116
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
117
118
|
if (!hostProjectName) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
try {
|
|
120
|
+
const packageJson = yield import(packageJsonPath);
|
|
121
|
+
hostProjectName = packageJson.name || null;
|
|
122
|
+
console.log("[ANALYTICS] Fetched project name from package details: ", hostProjectName);
|
|
123
|
+
enabled = isEnabled();
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
console.warn("[ANALYTICS] Failed to fetch project name, continuing without");
|
|
127
|
+
}
|
|
121
128
|
}
|
|
122
129
|
return hostProjectName;
|
|
123
130
|
});
|
|
@@ -127,6 +134,7 @@ function enrichEventData(data) {
|
|
|
127
134
|
data.eventId = uuidv4();
|
|
128
135
|
data.client = hostProjectName;
|
|
129
136
|
data.sessionId = getSessionId();
|
|
137
|
+
data.url = window.location.href;
|
|
130
138
|
region && (data.region = region);
|
|
131
139
|
address && !data.address && (data.address = address);
|
|
132
140
|
coordinates && !data.coordinates && (data.coordinates = coordinates);
|
|
@@ -148,7 +156,7 @@ export function sendBulkData(data, callback) {
|
|
|
148
156
|
function sendAnalyticsData(data) {
|
|
149
157
|
return __awaiter(this, void 0, void 0, function* () {
|
|
150
158
|
try {
|
|
151
|
-
if (!apiKey && !
|
|
159
|
+
if (!apiKey && !analyticsLogEndpoint) {
|
|
152
160
|
console.error('Neither of API key and Analytics server configured. Data not sent.');
|
|
153
161
|
return;
|
|
154
162
|
}
|
|
@@ -162,7 +170,7 @@ function sendAnalyticsData(data) {
|
|
|
162
170
|
if (apiKey) {
|
|
163
171
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
164
172
|
}
|
|
165
|
-
const logEndpoint = apiKey ? ARMCO_SERVER + ADD :
|
|
173
|
+
const logEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsLogEndpoint;
|
|
166
174
|
try {
|
|
167
175
|
const response = yield fetch(logEndpoint, options);
|
|
168
176
|
console.log('Analytics data sent to server:', logEndpoint, data, response.status, response.statusText);
|
|
@@ -179,7 +187,7 @@ function sendAnalyticsData(data) {
|
|
|
179
187
|
function tagEvents(email) {
|
|
180
188
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
189
|
try {
|
|
182
|
-
if (!apiKey && !
|
|
190
|
+
if (!apiKey && !analyticsTagEndpoint) {
|
|
183
191
|
console.error('Neither of API key and Analytics server configured. Tagging won\'t be attempted.');
|
|
184
192
|
return;
|
|
185
193
|
}
|
|
@@ -193,7 +201,7 @@ function tagEvents(email) {
|
|
|
193
201
|
if (apiKey) {
|
|
194
202
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
195
203
|
}
|
|
196
|
-
const tagEndpoint = apiKey ? ARMCO_SERVER + ADD :
|
|
204
|
+
const tagEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsTagEndpoint;
|
|
197
205
|
const response = yield fetch(tagEndpoint, options);
|
|
198
206
|
console.log('Analytics data sent to server:', tagEndpoint, response.status, response.statusText);
|
|
199
207
|
}
|
|
@@ -429,7 +437,8 @@ function loadAnalytics(config) {
|
|
|
429
437
|
if (CONFIG) {
|
|
430
438
|
console.log("[ANALYTICS] Configuration loaded");
|
|
431
439
|
apiKey = CONFIG.apiKey || null;
|
|
432
|
-
|
|
440
|
+
analyticsLogEndpoint = CONFIG.analyticsLogEndpoint || null;
|
|
441
|
+
analyticsTagEndpoint = CONFIG.analyticsTagEndpoint || null;
|
|
433
442
|
hostProjectName = CONFIG.hostProjectName || hostProjectName || null;
|
|
434
443
|
console.log("[ANALYTICS] Check if Analytics enabled");
|
|
435
444
|
enabled = isEnabled();
|
|
@@ -446,6 +455,10 @@ function loadAnalytics(config) {
|
|
|
446
455
|
const anonId = generateAnonymousId();
|
|
447
456
|
console.log("Tracking User as", anonId);
|
|
448
457
|
startSession();
|
|
458
|
+
window.addEventListener('load', function () {
|
|
459
|
+
console.log("[ANALYTICS] Logging page load");
|
|
460
|
+
trackEvent("PAGE");
|
|
461
|
+
});
|
|
449
462
|
}
|
|
450
463
|
else {
|
|
451
464
|
console.warn("[ANALYTICS] Analytics blocked by client, or disabled by configuration!");
|
package/constants.d.ts
CHANGED
package/constants.js
CHANGED
package/index.interface.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export interface Event {
|
|
|
14
14
|
}
|
|
15
15
|
export interface ConfigType {
|
|
16
16
|
apiKey?: string;
|
|
17
|
-
|
|
17
|
+
analyticsLogEndpoint?: string;
|
|
18
|
+
analyticsTagEndpoint?: string;
|
|
18
19
|
hostProjectName?: string;
|
|
19
20
|
trackEvents?: Array<string>;
|
|
20
21
|
submissionStrategy?: SubmissionStrategy;
|