@nommos/core 0.0.3
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/README.md +527 -0
- package/package.json +16 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +11 -0
- package/src/index.js.map +1 -0
- package/src/lib/constants.d.ts +11 -0
- package/src/lib/constants.js +55 -0
- package/src/lib/constants.js.map +1 -0
- package/src/lib/data-attribution.d.ts +65 -0
- package/src/lib/data-attribution.js +213 -0
- package/src/lib/data-attribution.js.map +1 -0
- package/src/lib/enums.d.ts +30 -0
- package/src/lib/enums.js +37 -0
- package/src/lib/enums.js.map +1 -0
- package/src/lib/event-tracker.d.ts +55 -0
- package/src/lib/event-tracker.js +174 -0
- package/src/lib/event-tracker.js.map +1 -0
- package/src/lib/types.d.ts +40 -0
- package/src/lib/types.js +3 -0
- package/src/lib/types.js.map +1 -0
- package/src/lib/utils.d.ts +24 -0
- package/src/lib/utils.js +52 -0
- package/src/lib/utils.js.map +1 -0
- package/src/lib/webSocket-client.d.ts +69 -0
- package/src/lib/webSocket-client.js +132 -0
- package/src/lib/webSocket-client.js.map +1 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { SITE_SOURCE } from './enums';
|
|
2
|
+
import { UrlTrackingKey } from './types';
|
|
3
|
+
export declare class DataAttribution {
|
|
4
|
+
private readonly urlTrackingKey;
|
|
5
|
+
constructor(urlTrackingKey: UrlTrackingKey);
|
|
6
|
+
private latestAffiliateCandidate;
|
|
7
|
+
private lastCapturedAffiliateIsNew;
|
|
8
|
+
private currentUserIP;
|
|
9
|
+
private readonly loadedUserIp;
|
|
10
|
+
loadedUserIp$: import("rxjs").Observable<boolean>;
|
|
11
|
+
private readonly hasParrainOrAffiliate;
|
|
12
|
+
hasParrainOrAffiliate$: import("rxjs").Observable<boolean>;
|
|
13
|
+
private readonly stripUrlTrackedParams;
|
|
14
|
+
stripUrlTrackedParams$: import("rxjs").Observable<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* Initializes the capturing of user's tracking parameters from the current url
|
|
17
|
+
* used in user's action tracking.
|
|
18
|
+
*
|
|
19
|
+
* @param currentUrl - url from which to capture user's tracking parameters
|
|
20
|
+
* @param options - option to get ride of the parameters from the url once successfully stored.
|
|
21
|
+
*/
|
|
22
|
+
init(currentUrl: string, options?: {
|
|
23
|
+
stripFromUrl?: boolean;
|
|
24
|
+
}): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns a persistent browser/device identifier.
|
|
27
|
+
*
|
|
28
|
+
* Logic:
|
|
29
|
+
* - If already in `localStorage`, return it.
|
|
30
|
+
* - Otherwise generate a UUID, store it, and set a cookie (`device_id`)
|
|
31
|
+
*
|
|
32
|
+
* Persistence: 1 year (via cookie), infinite via localStorage.
|
|
33
|
+
*/
|
|
34
|
+
get browserId(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the last fetched public IP address of the user.
|
|
37
|
+
* Empty string until `fetchIp()` completes successfully.
|
|
38
|
+
*/
|
|
39
|
+
get userIp(): string;
|
|
40
|
+
setLoadedIP(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Fetches for the current public address of the user (machine on which the application is used).
|
|
43
|
+
*/
|
|
44
|
+
fetchIp(): import("rxjs").Observable<void>;
|
|
45
|
+
get source(): SITE_SOURCE;
|
|
46
|
+
/** Current affiliate from session; empty string if absent. */
|
|
47
|
+
get affiliate(): string | null;
|
|
48
|
+
/** Current parrainage from session; empty string if absent. */
|
|
49
|
+
get parrainage(): string | null;
|
|
50
|
+
/**
|
|
51
|
+
* True only if the latest ?{{affiliateUrlKey}}= in the *current* URL is NOT already in the 1-year history.
|
|
52
|
+
* If no ?{{affiliateUrlKey}}= was present this navigation, returns false.
|
|
53
|
+
*/
|
|
54
|
+
get trackAffiliate(): boolean;
|
|
55
|
+
private captureTrackingParamsFromCurrentUrl;
|
|
56
|
+
private capturePartnerParams;
|
|
57
|
+
private getHistory;
|
|
58
|
+
private setHistory;
|
|
59
|
+
private historyHas;
|
|
60
|
+
private pushHistory;
|
|
61
|
+
/** Explicit reset if needed SOURCE_KEY. */
|
|
62
|
+
clearSourceSession(): void;
|
|
63
|
+
clearAffiliateSession(): void;
|
|
64
|
+
clearAffiliateHistory(): void;
|
|
65
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataAttribution = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const enums_1 = require("./enums");
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
class DataAttribution {
|
|
10
|
+
constructor(urlTrackingKey) {
|
|
11
|
+
this.urlTrackingKey = urlTrackingKey;
|
|
12
|
+
this.latestAffiliateCandidate = null;
|
|
13
|
+
this.lastCapturedAffiliateIsNew = false;
|
|
14
|
+
this.currentUserIP = '';
|
|
15
|
+
this.loadedUserIp = new rxjs_1.BehaviorSubject(false);
|
|
16
|
+
this.loadedUserIp$ = this.loadedUserIp.asObservable();
|
|
17
|
+
this.hasParrainOrAffiliate = new rxjs_1.BehaviorSubject(false);
|
|
18
|
+
this.hasParrainOrAffiliate$ = this.hasParrainOrAffiliate.asObservable();
|
|
19
|
+
this.stripUrlTrackedParams = new rxjs_1.BehaviorSubject(false);
|
|
20
|
+
this.stripUrlTrackedParams$ = this.stripUrlTrackedParams.asObservable();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Initializes the capturing of user's tracking parameters from the current url
|
|
24
|
+
* used in user's action tracking.
|
|
25
|
+
*
|
|
26
|
+
* @param currentUrl - url from which to capture user's tracking parameters
|
|
27
|
+
* @param options - option to get ride of the parameters from the url once successfully stored.
|
|
28
|
+
*/
|
|
29
|
+
init(currentUrl, options = {}) {
|
|
30
|
+
const { stripFromUrl = true } = options;
|
|
31
|
+
this.captureTrackingParamsFromCurrentUrl(stripFromUrl, currentUrl);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns a persistent browser/device identifier.
|
|
35
|
+
*
|
|
36
|
+
* Logic:
|
|
37
|
+
* - If already in `localStorage`, return it.
|
|
38
|
+
* - Otherwise generate a UUID, store it, and set a cookie (`device_id`)
|
|
39
|
+
*
|
|
40
|
+
* Persistence: 1 year (via cookie), infinite via localStorage.
|
|
41
|
+
*/
|
|
42
|
+
get browserId() {
|
|
43
|
+
let v = localStorage.getItem(constants_1.BROWSER_KEY);
|
|
44
|
+
if (!v) {
|
|
45
|
+
v = (0, uuid_1.v4)();
|
|
46
|
+
localStorage.setItem(constants_1.BROWSER_KEY, v);
|
|
47
|
+
document.cookie = `device_id=${v}; path=/; max-age=${365 * 24 * 60 * 60}`;
|
|
48
|
+
}
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns the last fetched public IP address of the user.
|
|
53
|
+
* Empty string until `fetchIp()` completes successfully.
|
|
54
|
+
*/
|
|
55
|
+
get userIp() {
|
|
56
|
+
return this.currentUserIP;
|
|
57
|
+
}
|
|
58
|
+
setLoadedIP() {
|
|
59
|
+
this.loadedUserIp.next(true);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Fetches for the current public address of the user (machine on which the application is used).
|
|
63
|
+
*/
|
|
64
|
+
fetchIp() {
|
|
65
|
+
return (0, rxjs_1.from)(fetch('https://api.ipify.org?format=json')).pipe((0, rxjs_1.switchMap)((res) => (0, rxjs_1.from)(res.json())), // flatten inner promise
|
|
66
|
+
(0, rxjs_1.map)((data) => {
|
|
67
|
+
var _a;
|
|
68
|
+
this.currentUserIP = (_a = data === null || data === void 0 ? void 0 : data.ip) !== null && _a !== void 0 ? _a : '';
|
|
69
|
+
}), // extract IP or ''
|
|
70
|
+
(0, rxjs_1.catchError)(() => (0, rxjs_1.of)()) // on any error, return ''
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
get source() {
|
|
74
|
+
const v = sessionStorage.getItem(constants_1.SOURCE_KEY);
|
|
75
|
+
return v != null && v.length > 0 ? v : enums_1.SITE_SOURCE.DIRECT;
|
|
76
|
+
}
|
|
77
|
+
/** Current affiliate from session; empty string if absent. */
|
|
78
|
+
get affiliate() {
|
|
79
|
+
const v = sessionStorage.getItem(constants_1.AFFILIATE_KEY);
|
|
80
|
+
return v != null && v.length > 0 ? v : null;
|
|
81
|
+
}
|
|
82
|
+
/** Current parrainage from session; empty string if absent. */
|
|
83
|
+
get parrainage() {
|
|
84
|
+
const v = sessionStorage.getItem(constants_1.PARRAINAGE_KEY);
|
|
85
|
+
return v != null && v.length > 0 ? v : null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* True only if the latest ?{{affiliateUrlKey}}= in the *current* URL is NOT already in the 1-year history.
|
|
89
|
+
* If no ?{{affiliateUrlKey}}= was present this navigation, returns false.
|
|
90
|
+
*/
|
|
91
|
+
get trackAffiliate() {
|
|
92
|
+
if (!this.latestAffiliateCandidate)
|
|
93
|
+
return false;
|
|
94
|
+
return this.lastCapturedAffiliateIsNew;
|
|
95
|
+
}
|
|
96
|
+
captureTrackingParamsFromCurrentUrl(stripFromUrl, currentUrl) {
|
|
97
|
+
const queryParams = (0, utils_1.getQueryParams)(currentUrl);
|
|
98
|
+
const source = queryParams === null || queryParams === void 0 ? void 0 : queryParams[this.urlTrackingKey.source];
|
|
99
|
+
let shouldStrip = false;
|
|
100
|
+
// Capture source (session-scoped)
|
|
101
|
+
if (source !== undefined) {
|
|
102
|
+
sessionStorage.setItem(constants_1.SOURCE_KEY, String(source).trim());
|
|
103
|
+
shouldStrip = true;
|
|
104
|
+
}
|
|
105
|
+
if (this.urlTrackingKey.parrainageKey || this.urlTrackingKey.affiliateKey) {
|
|
106
|
+
const parrainage = this.urlTrackingKey.parrainageKey
|
|
107
|
+
? queryParams === null || queryParams === void 0 ? void 0 : queryParams[this.urlTrackingKey.parrainageKey]
|
|
108
|
+
: undefined; // parrainage code
|
|
109
|
+
const affiliate = this.urlTrackingKey.affiliateKey
|
|
110
|
+
? queryParams === null || queryParams === void 0 ? void 0 : queryParams[this.urlTrackingKey.affiliateKey]
|
|
111
|
+
: undefined; // affiliate code
|
|
112
|
+
shouldStrip = affiliate !== undefined;
|
|
113
|
+
this.capturePartnerParams(parrainage, affiliate);
|
|
114
|
+
}
|
|
115
|
+
// Ensure default source
|
|
116
|
+
if (!sessionStorage.getItem(constants_1.SOURCE_KEY)) {
|
|
117
|
+
sessionStorage.setItem(constants_1.SOURCE_KEY, enums_1.SITE_SOURCE.DIRECT);
|
|
118
|
+
}
|
|
119
|
+
// Ensure affiliate key exists in session
|
|
120
|
+
if (!sessionStorage.getItem(constants_1.AFFILIATE_KEY)) {
|
|
121
|
+
sessionStorage.setItem(constants_1.AFFILIATE_KEY, '');
|
|
122
|
+
}
|
|
123
|
+
// Optionally strip params we consumed
|
|
124
|
+
if (stripFromUrl && shouldStrip) {
|
|
125
|
+
this.stripUrlTrackedParams.next(true);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.stripUrlTrackedParams.next(false);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
capturePartnerParams(parrainage, affiliate) {
|
|
132
|
+
if (this.urlTrackingKey.parrainageKey) {
|
|
133
|
+
// Capture affiliate: keep current in session, and record to 1-year history
|
|
134
|
+
if (parrainage !== undefined) {
|
|
135
|
+
const rawParrainage = String(parrainage).trim();
|
|
136
|
+
sessionStorage.setItem(constants_1.PARRAINAGE_KEY, rawParrainage);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (this.urlTrackingKey.affiliateKey) {
|
|
140
|
+
this.latestAffiliateCandidate = null;
|
|
141
|
+
this.lastCapturedAffiliateIsNew = false;
|
|
142
|
+
if (affiliate !== undefined) {
|
|
143
|
+
const affiliateRef = String(affiliate).trim();
|
|
144
|
+
sessionStorage.setItem(constants_1.AFFILIATE_KEY, affiliateRef);
|
|
145
|
+
const lc = (0, utils_1.normString)(affiliateRef);
|
|
146
|
+
if (lc.length > 0) {
|
|
147
|
+
const isInHistory = this.historyHas(lc);
|
|
148
|
+
this.latestAffiliateCandidate = lc;
|
|
149
|
+
this.lastCapturedAffiliateIsNew = !isInHistory;
|
|
150
|
+
// Push to history (refresh TTL on every update)
|
|
151
|
+
this.pushHistory(lc);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (parrainage !== undefined || affiliate !== undefined) {
|
|
156
|
+
this.hasParrainOrAffiliate.next(true);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.hasParrainOrAffiliate.next(false);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/* ---------- History (array with TTL in localStorage) ---------- */
|
|
163
|
+
getHistory() {
|
|
164
|
+
const raw = localStorage.getItem(constants_1.AFFILIATE_HISTORY_KEY);
|
|
165
|
+
if (!raw)
|
|
166
|
+
return [];
|
|
167
|
+
try {
|
|
168
|
+
const parsed = JSON.parse(raw);
|
|
169
|
+
if (!parsed || typeof parsed.e !== 'number' || !Array.isArray(parsed.v)) {
|
|
170
|
+
localStorage.removeItem(constants_1.AFFILIATE_HISTORY_KEY);
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
if (Date.now() > parsed.e) {
|
|
174
|
+
localStorage.removeItem(constants_1.AFFILIATE_HISTORY_KEY);
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
// Ensure strings + lowercased normalization
|
|
178
|
+
return parsed.v.map((x) => (0, utils_1.normString)(String(x)));
|
|
179
|
+
}
|
|
180
|
+
catch (_a) {
|
|
181
|
+
localStorage.removeItem(constants_1.AFFILIATE_HISTORY_KEY);
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
setHistory(arr) {
|
|
186
|
+
const unique = Array.from(new Set(arr.map(utils_1.normString)));
|
|
187
|
+
const payload = { v: unique, e: Date.now() + constants_1.ONE_YEAR_MS };
|
|
188
|
+
localStorage.setItem(constants_1.AFFILIATE_HISTORY_KEY, JSON.stringify(payload));
|
|
189
|
+
}
|
|
190
|
+
historyHas(codeLc) {
|
|
191
|
+
return this.getHistory().includes((0, utils_1.normString)(codeLc));
|
|
192
|
+
}
|
|
193
|
+
pushHistory(codeLc) {
|
|
194
|
+
const hist = this.getHistory();
|
|
195
|
+
if (!hist.includes((0, utils_1.normString)(codeLc))) {
|
|
196
|
+
hist.push((0, utils_1.normString)(codeLc));
|
|
197
|
+
}
|
|
198
|
+
// Always refresh TTL (rolling 1-year window)
|
|
199
|
+
this.setHistory(hist);
|
|
200
|
+
}
|
|
201
|
+
/** Explicit reset if needed SOURCE_KEY. */
|
|
202
|
+
clearSourceSession() {
|
|
203
|
+
sessionStorage.removeItem(constants_1.SOURCE_KEY);
|
|
204
|
+
}
|
|
205
|
+
clearAffiliateSession() {
|
|
206
|
+
sessionStorage.removeItem(constants_1.AFFILIATE_KEY);
|
|
207
|
+
}
|
|
208
|
+
clearAffiliateHistory() {
|
|
209
|
+
localStorage.removeItem(constants_1.AFFILIATE_HISTORY_KEY);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
exports.DataAttribution = DataAttribution;
|
|
213
|
+
//# sourceMappingURL=data-attribution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-attribution.js","sourceRoot":"","sources":["../../../../../nommos/core/src/lib/data-attribution.ts"],"names":[],"mappings":";;;AAAA,+BAA6E;AAC7E,2CAOqB;AACrB,mCAAsC;AAEtC,mCAAqD;AACrD,+BAAoC;AAEpC,MAAa,eAAe;IAC1B,YAA6B,cAA8B;QAA9B,mBAAc,GAAd,cAAc,CAAgB;QAEnD,6BAAwB,GAAkB,IAAI,CAAC;QAC/C,+BAA0B,GAAG,KAAK,CAAC;QACnC,kBAAa,GAAG,EAAE,CAAC;QACV,iBAAY,GAAG,IAAI,sBAAe,CAAU,KAAK,CAAC,CA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|
+
SIGNIN = "SIGNIN",
|
|
4
|
+
REGISTRATION = "REGISTRATION",
|
|
5
|
+
BET = "BET",
|
|
6
|
+
MENU = "MENU",
|
|
7
|
+
TOURNAMENT = "TOURNAMENT",
|
|
8
|
+
SPORT = "SPORT",
|
|
9
|
+
DEPOSIT = "DEPOSIT",
|
|
10
|
+
VISIT = "VISIT",
|
|
11
|
+
COUPON = "COUPON"
|
|
12
|
+
}
|
|
13
|
+
/** States of analytics actions carried out by user */
|
|
14
|
+
export declare enum ANALYTICS_ACTION_STATE {
|
|
15
|
+
SELECT = "SELECT",
|
|
16
|
+
UNSELECT = "UN_SELECT",
|
|
17
|
+
SUBMIT = "SUBMIT",
|
|
18
|
+
SUCCESS = "SUCCESS",
|
|
19
|
+
FAILURE = "FAILURE",
|
|
20
|
+
INIT = "INIT"
|
|
21
|
+
}
|
|
22
|
+
/** Source from which the user originates to access the current site. */
|
|
23
|
+
export declare enum SITE_SOURCE {
|
|
24
|
+
FACEBOOK = "FACEBOOK",
|
|
25
|
+
INSTAGRAM = "INSTAGRAM",
|
|
26
|
+
TWITTER = "TWITTER",
|
|
27
|
+
TELEGRAM = "TELEGRAM",
|
|
28
|
+
DIRECT = "DIRECT",
|
|
29
|
+
SMS = "SMS"
|
|
30
|
+
}
|
package/src/lib/enums.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SITE_SOURCE = exports.ANALYTICS_ACTION_STATE = exports.ANALYTICS_ACTION = void 0;
|
|
4
|
+
/** Analytics Actions carried out by user to be tracked */
|
|
5
|
+
var ANALYTICS_ACTION;
|
|
6
|
+
(function (ANALYTICS_ACTION) {
|
|
7
|
+
ANALYTICS_ACTION["SIGNIN"] = "SIGNIN";
|
|
8
|
+
ANALYTICS_ACTION["REGISTRATION"] = "REGISTRATION";
|
|
9
|
+
ANALYTICS_ACTION["BET"] = "BET";
|
|
10
|
+
ANALYTICS_ACTION["MENU"] = "MENU";
|
|
11
|
+
ANALYTICS_ACTION["TOURNAMENT"] = "TOURNAMENT";
|
|
12
|
+
ANALYTICS_ACTION["SPORT"] = "SPORT";
|
|
13
|
+
ANALYTICS_ACTION["DEPOSIT"] = "DEPOSIT";
|
|
14
|
+
ANALYTICS_ACTION["VISIT"] = "VISIT";
|
|
15
|
+
ANALYTICS_ACTION["COUPON"] = "COUPON";
|
|
16
|
+
})(ANALYTICS_ACTION || (exports.ANALYTICS_ACTION = ANALYTICS_ACTION = {}));
|
|
17
|
+
/** States of analytics actions carried out by user */
|
|
18
|
+
var ANALYTICS_ACTION_STATE;
|
|
19
|
+
(function (ANALYTICS_ACTION_STATE) {
|
|
20
|
+
ANALYTICS_ACTION_STATE["SELECT"] = "SELECT";
|
|
21
|
+
ANALYTICS_ACTION_STATE["UNSELECT"] = "UN_SELECT";
|
|
22
|
+
ANALYTICS_ACTION_STATE["SUBMIT"] = "SUBMIT";
|
|
23
|
+
ANALYTICS_ACTION_STATE["SUCCESS"] = "SUCCESS";
|
|
24
|
+
ANALYTICS_ACTION_STATE["FAILURE"] = "FAILURE";
|
|
25
|
+
ANALYTICS_ACTION_STATE["INIT"] = "INIT";
|
|
26
|
+
})(ANALYTICS_ACTION_STATE || (exports.ANALYTICS_ACTION_STATE = ANALYTICS_ACTION_STATE = {}));
|
|
27
|
+
/** Source from which the user originates to access the current site. */
|
|
28
|
+
var SITE_SOURCE;
|
|
29
|
+
(function (SITE_SOURCE) {
|
|
30
|
+
SITE_SOURCE["FACEBOOK"] = "FACEBOOK";
|
|
31
|
+
SITE_SOURCE["INSTAGRAM"] = "INSTAGRAM";
|
|
32
|
+
SITE_SOURCE["TWITTER"] = "TWITTER";
|
|
33
|
+
SITE_SOURCE["TELEGRAM"] = "TELEGRAM";
|
|
34
|
+
SITE_SOURCE["DIRECT"] = "DIRECT";
|
|
35
|
+
SITE_SOURCE["SMS"] = "SMS";
|
|
36
|
+
})(SITE_SOURCE || (exports.SITE_SOURCE = SITE_SOURCE = {}));
|
|
37
|
+
//# sourceMappingURL=enums.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.js","sourceRoot":"","sources":["../../../../../nommos/core/src/lib/enums.ts"],"names":[],"mappings":";;;AAAA,2DAA2D;AAC3D,IAAY,gBAUX;AAVD,WAAY,gBAAgB;IAC1B,qCAAiB,CAAA;IACjB,iDAA6B,CAAA;IAC7B,+BAAW,CAAA;IACX,iCAAa,CAAA;IACb,6CAAyB,CAAA;IACzB,mCAAe,CAAA;IACf,uCAAmB,CAAA;IACnB,mCAAe,CAAA;IACf,qCAAiB,CAAA;AACnB,CAAC,EAVW,gBAAgB,gCAAhB,gBAAgB,QAU3B;AAED,uDAAuD;AACvD,IAAY,sBAOX;AAPD,WAAY,sBAAsB;IAChC,2CAAiB,CAAA;IACjB,gDAAsB,CAAA;IACtB,2CAAiB,CAAA;IACjB,6CAAmB,CAAA;IACnB,6CAAmB,CAAA;IACnB,uCAAa,CAAA;AACf,CAAC,EAPW,sBAAsB,sCAAtB,sBAAsB,QAOjC;AAED,wEAAwE;AACxE,IAAY,WAOX;AAPD,WAAY,WAAW;IACrB,oCAAqB,CAAA;IACrB,sCAAuB,CAAA;IACvB,kCAAmB,CAAA;IACnB,oCAAqB,CAAA;IACrB,gCAAiB,CAAA;IACjB,0BAAW,CAAA;AACb,CAAC,EAPW,WAAW,2BAAX,WAAW,QAOtB"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AnalyticsEvent, ConnectionConfig, UrlTrackingKey } from './types';
|
|
2
|
+
import { ANALYTICS_ACTION, ANALYTICS_ACTION_STATE } from './enums';
|
|
3
|
+
export declare class EventTracker {
|
|
4
|
+
private readonly urlTrackingKey;
|
|
5
|
+
private readonly ws;
|
|
6
|
+
private readonly da;
|
|
7
|
+
private ipSubscription;
|
|
8
|
+
private eventSubscription;
|
|
9
|
+
private webSocketConnectedSubscription;
|
|
10
|
+
private readonly analyticsEvents;
|
|
11
|
+
analyticsEvents$: import("rxjs").Observable<AnalyticsEvent>;
|
|
12
|
+
/**
|
|
13
|
+
* Internal state map used to track SELECT/UNSELECT status of toggleable actions.
|
|
14
|
+
*/
|
|
15
|
+
private actionSelectState;
|
|
16
|
+
private readonly cfg;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new analytics tracker instance and initializes the attribution
|
|
19
|
+
* engine and WebSocket connection if tracking is enabled.
|
|
20
|
+
*/
|
|
21
|
+
constructor(config: ConnectionConfig, urlTrackingKey: UrlTrackingKey);
|
|
22
|
+
/**
|
|
23
|
+
* Returns whether tracking is enabled at runtime.
|
|
24
|
+
*/
|
|
25
|
+
trackingIsEnabled(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves the SELECT/UNSELECT state for a given analytics action.
|
|
28
|
+
*/
|
|
29
|
+
getActionSelectState(action: ANALYTICS_ACTION): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the SELECT/UNSELECT state for an analytics action.
|
|
32
|
+
*/
|
|
33
|
+
setActionSelectState(action: ANALYTICS_ACTION, selected: boolean): void;
|
|
34
|
+
/**
|
|
35
|
+
* Records a user interaction event and emits it into the analytics pipeline.
|
|
36
|
+
*
|
|
37
|
+
* This method:
|
|
38
|
+
* - Resolves the correct SELECT/UNSELECT state
|
|
39
|
+
* - Enriches the event with attribution information
|
|
40
|
+
* - Extracts browser/channel/OS from the user agent
|
|
41
|
+
* - Publishes the event to s pipeline for storage
|
|
42
|
+
*
|
|
43
|
+
* @param action - The action being tracked
|
|
44
|
+
* @param state - Initial state (SELECT/UNSELECT). Defaults to `SELECT`.
|
|
45
|
+
* @param data - Optional information about the action
|
|
46
|
+
* @param toggle - Whether to toggle selection state automatically for this action
|
|
47
|
+
* @param selected - Optional explicit override of selection state for this action
|
|
48
|
+
* @param userId - User identifier
|
|
49
|
+
* @param currentUrl - URL of the page where the action was carried out by the user
|
|
50
|
+
*/
|
|
51
|
+
trackEvent(action: ANALYTICS_ACTION, state?: ANALYTICS_ACTION_STATE, data?: Record<string, unknown> | null, toggle?: boolean, selected?: boolean | null, userId?: string, currentUrl?: string): void;
|
|
52
|
+
private init;
|
|
53
|
+
private fetchConfig;
|
|
54
|
+
unsubscribe(): void;
|
|
55
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventTracker = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const webSocket_client_1 = require("./webSocket-client");
|
|
6
|
+
const data_attribution_1 = require("./data-attribution");
|
|
7
|
+
const enums_1 = require("./enums");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const constants_1 = require("./constants");
|
|
10
|
+
class EventTracker {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new analytics tracker instance and initializes the attribution
|
|
13
|
+
* engine and WebSocket connection if tracking is enabled.
|
|
14
|
+
*/
|
|
15
|
+
constructor(config, urlTrackingKey) {
|
|
16
|
+
this.urlTrackingKey = urlTrackingKey;
|
|
17
|
+
this.ipSubscription = null;
|
|
18
|
+
this.eventSubscription = null;
|
|
19
|
+
this.webSocketConnectedSubscription = null;
|
|
20
|
+
this.analyticsEvents = new rxjs_1.Subject();
|
|
21
|
+
this.analyticsEvents$ = this.analyticsEvents.asObservable();
|
|
22
|
+
/**
|
|
23
|
+
* Internal state map used to track SELECT/UNSELECT status of toggleable actions.
|
|
24
|
+
*/
|
|
25
|
+
this.actionSelectState = {
|
|
26
|
+
BET: false,
|
|
27
|
+
MENU: false,
|
|
28
|
+
REGISTRATION: false,
|
|
29
|
+
SIGNIN: false,
|
|
30
|
+
SPORT: false,
|
|
31
|
+
TOURNAMENT: false,
|
|
32
|
+
VISIT: false,
|
|
33
|
+
DEPOSIT: false,
|
|
34
|
+
COUPON: false,
|
|
35
|
+
};
|
|
36
|
+
this.cfg = Object.assign({}, config);
|
|
37
|
+
this.ws = new webSocket_client_1.WebSocketClient();
|
|
38
|
+
this.da = new data_attribution_1.DataAttribution(urlTrackingKey);
|
|
39
|
+
this.init(this.cfg);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns whether tracking is enabled at runtime.
|
|
43
|
+
*/
|
|
44
|
+
trackingIsEnabled() {
|
|
45
|
+
return !!this.cfg.enableTracking;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves the SELECT/UNSELECT state for a given analytics action.
|
|
49
|
+
*/
|
|
50
|
+
getActionSelectState(action) {
|
|
51
|
+
return this.actionSelectState[action];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Sets the SELECT/UNSELECT state for an analytics action.
|
|
55
|
+
*/
|
|
56
|
+
setActionSelectState(action, selected) {
|
|
57
|
+
this.actionSelectState[action] = selected;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Records a user interaction event and emits it into the analytics pipeline.
|
|
61
|
+
*
|
|
62
|
+
* This method:
|
|
63
|
+
* - Resolves the correct SELECT/UNSELECT state
|
|
64
|
+
* - Enriches the event with attribution information
|
|
65
|
+
* - Extracts browser/channel/OS from the user agent
|
|
66
|
+
* - Publishes the event to s pipeline for storage
|
|
67
|
+
*
|
|
68
|
+
* @param action - The action being tracked
|
|
69
|
+
* @param state - Initial state (SELECT/UNSELECT). Defaults to `SELECT`.
|
|
70
|
+
* @param data - Optional information about the action
|
|
71
|
+
* @param toggle - Whether to toggle selection state automatically for this action
|
|
72
|
+
* @param selected - Optional explicit override of selection state for this action
|
|
73
|
+
* @param userId - User identifier
|
|
74
|
+
* @param currentUrl - URL of the page where the action was carried out by the user
|
|
75
|
+
*/
|
|
76
|
+
trackEvent(action, state = enums_1.ANALYTICS_ACTION_STATE.SELECT, data = null, toggle = true, selected = null, userId = '', currentUrl = '') {
|
|
77
|
+
if (state === enums_1.ANALYTICS_ACTION_STATE.SELECT) {
|
|
78
|
+
const newState = selected !== null && selected !== void 0 ? selected : (toggle
|
|
79
|
+
? !this.getActionSelectState(action)
|
|
80
|
+
: this.getActionSelectState(action));
|
|
81
|
+
this.setActionSelectState(action, newState);
|
|
82
|
+
state = newState
|
|
83
|
+
? enums_1.ANALYTICS_ACTION_STATE.SELECT
|
|
84
|
+
: enums_1.ANALYTICS_ACTION_STATE.UNSELECT;
|
|
85
|
+
}
|
|
86
|
+
else if (state === enums_1.ANALYTICS_ACTION_STATE.UNSELECT) {
|
|
87
|
+
this.setActionSelectState(action, false);
|
|
88
|
+
}
|
|
89
|
+
const { channel, os, browser } = (0, utils_1.userAgentInfo)(navigator.userAgent || '');
|
|
90
|
+
const eventTracked = Object.assign(Object.assign({ ip: this.da.userIp, channel, timeStamp: Date.now(), source: this.da.source, action,
|
|
91
|
+
state, uid: userId, bid: this.da.browserId, brandId: this.cfg.brandId, meta: {
|
|
92
|
+
os,
|
|
93
|
+
browser,
|
|
94
|
+
page: currentUrl,
|
|
95
|
+
} }, (data && { data })), (this.da.affiliate && { affiliateCode: this.da.affiliate }));
|
|
96
|
+
console.log(eventTracked);
|
|
97
|
+
this.analyticsEvents.next(eventTracked);
|
|
98
|
+
}
|
|
99
|
+
init(cfg) {
|
|
100
|
+
if (cfg.token) {
|
|
101
|
+
const ipFetch$ = this.da.fetchIp();
|
|
102
|
+
const configFetch$ = this.fetchConfig(cfg.token);
|
|
103
|
+
this.ipSubscription = (0, rxjs_1.forkJoin)([ipFetch$, configFetch$]).subscribe(() => {
|
|
104
|
+
this.da.setLoadedIP();
|
|
105
|
+
if (!this.cfg.enableTracking) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
for (const e of this.ws.pendingEvents) {
|
|
109
|
+
e.ip = this.da.userIp;
|
|
110
|
+
e.brandId = this.cfg.brandId;
|
|
111
|
+
}
|
|
112
|
+
if (cfg.rabbitUrl && cfg.username && cfg.password) {
|
|
113
|
+
this.ws.connectAnalyticsStomp(cfg);
|
|
114
|
+
}
|
|
115
|
+
else if (cfg.token) {
|
|
116
|
+
// Only log error if we expected to get config via token but failed or got incomplete data
|
|
117
|
+
console.error('[Nommos Tracker] Missing connection configuration after bootstrap.');
|
|
118
|
+
}
|
|
119
|
+
if (this.ipSubscription) {
|
|
120
|
+
this.ipSubscription.unsubscribe();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
this.eventSubscription = this.analyticsEvents$.subscribe((track) => {
|
|
124
|
+
this.ws.publishAnalyticsMessage(track);
|
|
125
|
+
});
|
|
126
|
+
this.webSocketConnectedSubscription =
|
|
127
|
+
this.ws.stompAnalyticsConnected$.subscribe((connected) => {
|
|
128
|
+
var _a;
|
|
129
|
+
if (connected) {
|
|
130
|
+
// Try to gracefully close on tab unload
|
|
131
|
+
window.addEventListener('beforeunload', () => {
|
|
132
|
+
try {
|
|
133
|
+
this.ws.disconnect();
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
console.error('[DME Analytics SDK] disconnect error:', error);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
(_a = this.webSocketConnectedSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
fetchConfig(token) {
|
|
145
|
+
const url = `${constants_1.CONFIG_ENDPOINT}/${token}`;
|
|
146
|
+
return (0, rxjs_1.from)(fetch(url)).pipe((0, rxjs_1.switchMap)((res) => {
|
|
147
|
+
if (!res.ok) {
|
|
148
|
+
throw new Error(`Failed to fetch config: ${res.statusText}`);
|
|
149
|
+
}
|
|
150
|
+
return (0, rxjs_1.from)(res.json());
|
|
151
|
+
}), (0, rxjs_1.map)((data) => {
|
|
152
|
+
this.cfg.enableTracking = data.enabled !== false;
|
|
153
|
+
if (this.cfg.enableTracking === false) {
|
|
154
|
+
console.warn('[Nommos Tracker] the events tracking feature is disabled for your token.');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
this.cfg.rabbitUrl = data.rabbit_url;
|
|
158
|
+
this.cfg.username = data.rabbit_username;
|
|
159
|
+
this.cfg.password = data.rabbit_password;
|
|
160
|
+
this.cfg.brandId = String(data.brand_id);
|
|
161
|
+
}), (0, rxjs_1.catchError)((err) => {
|
|
162
|
+
console.error('[Nommos Tracker] Error fetching from the endpoint:', err);
|
|
163
|
+
return (0, rxjs_1.of)(null);
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
unsubscribe() {
|
|
167
|
+
var _a, _b, _c;
|
|
168
|
+
(_a = this.ipSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
169
|
+
(_b = this.eventSubscription) === null || _b === void 0 ? void 0 : _b.unsubscribe();
|
|
170
|
+
(_c = this.webSocketConnectedSubscription) === null || _c === void 0 ? void 0 : _c.unsubscribe();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.EventTracker = EventTracker;
|
|
174
|
+
//# sourceMappingURL=event-tracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-tracker.js","sourceRoot":"","sources":["../../../../../nommos/core/src/lib/event-tracker.ts"],"names":[],"mappings":";;;AAAA,+BAA6F;AAC7F,yDAAqD;AACrD,yDAAqD;AAQrD,mCAAmE;AACnE,mCAAwC;AACxC,2CAA8C;AAE9C,MAAa,YAAY;IA0BvB;;;OAGG;IACH,YACE,MAAwB,EACP,cAA8B;QAA9B,mBAAc,GAAd,cAAc,CAAgB;QA7BzC,mBAAc,GAAwB,IAAI,CAAC;QAC3C,sBAAiB,GAAwB,IAAI,CAAC;QAC9C,mCAA8B,GAAwB,IAAI,CAAC;QAClD,oBAAe,GAAG,IAAI,cAAO,EAAkB,CAAC;QAC1D,qBAAgB,GAAG,IAAI,CAAC,eAAe,CAAC,YAAY,EAAE,CAAC;QAE9D;;WAEG;QACK,sBAAiB,GAAsB;YAC7C,GAAG,EAAE,KAAK;YACV,IAAI,EAAE,KAAK;YACX,YAAY,EAAE,KAAK;YACnB,MAAM,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,CAAC,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC;YACzC,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC;YACzC,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC3C,CAAC,CAAC,EACF,IAAA,iBAAU,EAAC,CAAC,GAAQ,EAAE,EAAE;YACtB,OAAO,CAAC,KAAK,CAAC,oDAAoD,EAAE,GAAG,CAAC,CAAC;YACzE,OAAO,IAAA,SAAE,EAAC,IAAI,CAAC,CAAC;QAClB,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;IAED,WAAW;;QACT,MAAA,IAAI,CAAC,cAAc,0CAAE,WAAW,EAAE,CAAC;QACnC,MAAA,IAAI,CAAC,iBAAiB,0CAAE,WAAW,EAAE,CAAC;QACtC,MAAA,IAAI,CAAC,8BAA8B,0CAAE,WAAW,EAAE,CAAC;IACrD,CAAC;CAEF;AA3ND,oCA2NC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ANALYTICS_ACTION, ANALYTICS_ACTION_STATE, SITE_SOURCE } from './enums';
|
|
2
|
+
export interface ConnectionConfig {
|
|
3
|
+
token: string;
|
|
4
|
+
mode: string;
|
|
5
|
+
}
|
|
6
|
+
/** @internal */
|
|
7
|
+
export interface FullConfig extends ConnectionConfig {
|
|
8
|
+
rabbitUrl?: string;
|
|
9
|
+
username?: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
brandId?: string;
|
|
12
|
+
appId?: string;
|
|
13
|
+
reconnectDelay?: number;
|
|
14
|
+
enableTracking?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface UrlTrackingKey {
|
|
17
|
+
source: string;
|
|
18
|
+
affiliateKey?: string;
|
|
19
|
+
parrainageKey?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface AnalyticsEvent {
|
|
22
|
+
timeStamp: number;
|
|
23
|
+
channel: number;
|
|
24
|
+
source: SITE_SOURCE;
|
|
25
|
+
action: ANALYTICS_ACTION;
|
|
26
|
+
state: ANALYTICS_ACTION_STATE;
|
|
27
|
+
affiliateCode?: string;
|
|
28
|
+
uid: string;
|
|
29
|
+
bid: string;
|
|
30
|
+
ip?: string;
|
|
31
|
+
brandId?: string;
|
|
32
|
+
data?: Record<string, unknown>;
|
|
33
|
+
meta?: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
export interface TTLStringArray {
|
|
36
|
+
v: string[];
|
|
37
|
+
e: number;
|
|
38
|
+
}
|
|
39
|
+
export type ActionSelectState = Record<ANALYTICS_ACTION, boolean>;
|
|
40
|
+
export type UaDef = [RegExp, string, ((m: RegExpMatchArray) => string)?];
|
package/src/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../nommos/core/src/lib/types.ts"],"names":[],"mappings":""}
|