@dereekb/analytics 13.4.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/index.cjs.default.js +1 -0
- package/index.cjs.js +68 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -0
- package/index.esm.js +66 -0
- package/nestjs/index.cjs.default.js +1 -0
- package/nestjs/index.cjs.js +646 -0
- package/nestjs/index.cjs.mjs +2 -0
- package/nestjs/index.d.ts +1 -0
- package/nestjs/index.esm.js +641 -0
- package/nestjs/package.json +30 -0
- package/nestjs/src/index.d.ts +1 -0
- package/nestjs/src/lib/index.d.ts +2 -0
- package/nestjs/src/lib/interceptor/analytics.interceptor.d.ts +65 -0
- package/nestjs/src/lib/interceptor/index.d.ts +1 -0
- package/nestjs/src/lib/segment/index.d.ts +5 -0
- package/nestjs/src/lib/segment/segment.api.d.ts +15 -0
- package/nestjs/src/lib/segment/segment.config.d.ts +37 -0
- package/nestjs/src/lib/segment/segment.module.d.ts +28 -0
- package/nestjs/src/lib/segment/segment.service.d.ts +62 -0
- package/nestjs/src/lib/segment/segment.type.d.ts +26 -0
- package/package.json +32 -0
- package/src/index.d.ts +1 -0
- package/src/lib/analytics.event.d.ts +62 -0
- package/src/lib/analytics.event.util.d.ts +55 -0
- package/src/lib/index.d.ts +2 -0
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var common = require('@nestjs/common');
|
|
4
|
+
var analyticsNode = require('@segment/analytics-node');
|
|
5
|
+
var config = require('@nestjs/config');
|
|
6
|
+
var nestjs = require('@dereekb/nestjs');
|
|
7
|
+
var operators = require('rxjs/operators');
|
|
8
|
+
|
|
9
|
+
function _type_of(obj) {
|
|
10
|
+
"@swc/helpers - typeof";
|
|
11
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
12
|
+
}
|
|
13
|
+
function __decorate(decorators, target, key, desc) {
|
|
14
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
15
|
+
if ((typeof Reflect === "undefined" ? "undefined" : _type_of(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
16
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
17
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
18
|
+
}
|
|
19
|
+
function __param(paramIndex, decorator) {
|
|
20
|
+
return function(target, key) {
|
|
21
|
+
decorator(target, key, paramIndex);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
25
|
+
var e = new Error(message);
|
|
26
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function _class_call_check$4(instance, Constructor) {
|
|
30
|
+
if (!(instance instanceof Constructor)) {
|
|
31
|
+
throw new TypeError("Cannot call a class as a function");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function _defineProperties$3(target, props) {
|
|
35
|
+
for(var i = 0; i < props.length; i++){
|
|
36
|
+
var descriptor = props[i];
|
|
37
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
38
|
+
descriptor.configurable = true;
|
|
39
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
40
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function _create_class$3(Constructor, protoProps, staticProps) {
|
|
44
|
+
if (staticProps) _defineProperties$3(Constructor, staticProps);
|
|
45
|
+
return Constructor;
|
|
46
|
+
}
|
|
47
|
+
function _define_property$3(obj, key, value) {
|
|
48
|
+
if (key in obj) {
|
|
49
|
+
Object.defineProperty(obj, key, {
|
|
50
|
+
value: value,
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
obj[key] = value;
|
|
57
|
+
}
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Configuration for the Segment analytics service.
|
|
62
|
+
*/ var SegmentServiceConfig = /*#__PURE__*/ function() {
|
|
63
|
+
function SegmentServiceConfig() {
|
|
64
|
+
_class_call_check$4(this, SegmentServiceConfig);
|
|
65
|
+
/**
|
|
66
|
+
* Segment write key for the source.
|
|
67
|
+
*/ _define_property$3(this, "writeKey", void 0);
|
|
68
|
+
/**
|
|
69
|
+
* When true, events are logged to the console instead of sent to Segment.
|
|
70
|
+
*/ _define_property$3(this, "logOnly", void 0);
|
|
71
|
+
/**
|
|
72
|
+
* Optional application context included in all Segment event contexts.
|
|
73
|
+
*/ _define_property$3(this, "appContext", void 0);
|
|
74
|
+
}
|
|
75
|
+
_create_class$3(SegmentServiceConfig, null, [
|
|
76
|
+
{
|
|
77
|
+
key: "assertValidConfig",
|
|
78
|
+
value: /**
|
|
79
|
+
* Validates that the given config has the required fields (e.g., a non-empty write key).
|
|
80
|
+
*
|
|
81
|
+
* @param config - The config instance to validate.
|
|
82
|
+
* @throws {Error} When the write key is missing or empty.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* SegmentServiceConfig.assertValidConfig(config);
|
|
87
|
+
* ```
|
|
88
|
+
*/ function assertValidConfig(config) {
|
|
89
|
+
if (!config.writeKey) {
|
|
90
|
+
throw new Error('No Segment write key specified.');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
]);
|
|
95
|
+
return SegmentServiceConfig;
|
|
96
|
+
}
|
|
97
|
+
();
|
|
98
|
+
|
|
99
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
100
|
+
try {
|
|
101
|
+
var info = gen[key](arg);
|
|
102
|
+
var value = info.value;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
reject(error);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (info.done) {
|
|
108
|
+
resolve(value);
|
|
109
|
+
} else {
|
|
110
|
+
Promise.resolve(value).then(_next, _throw);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function _async_to_generator(fn) {
|
|
114
|
+
return function() {
|
|
115
|
+
var self = this, args = arguments;
|
|
116
|
+
return new Promise(function(resolve, reject) {
|
|
117
|
+
var gen = fn.apply(self, args);
|
|
118
|
+
function _next(value) {
|
|
119
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
120
|
+
}
|
|
121
|
+
function _throw(err) {
|
|
122
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
123
|
+
}
|
|
124
|
+
_next(undefined);
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function _class_call_check$3(instance, Constructor) {
|
|
129
|
+
if (!(instance instanceof Constructor)) {
|
|
130
|
+
throw new TypeError("Cannot call a class as a function");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function _defineProperties$2(target, props) {
|
|
134
|
+
for(var i = 0; i < props.length; i++){
|
|
135
|
+
var descriptor = props[i];
|
|
136
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
137
|
+
descriptor.configurable = true;
|
|
138
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
139
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function _create_class$2(Constructor, protoProps, staticProps) {
|
|
143
|
+
if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
|
|
144
|
+
return Constructor;
|
|
145
|
+
}
|
|
146
|
+
function _define_property$2(obj, key, value) {
|
|
147
|
+
if (key in obj) {
|
|
148
|
+
Object.defineProperty(obj, key, {
|
|
149
|
+
value: value,
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true,
|
|
152
|
+
writable: true
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
obj[key] = value;
|
|
156
|
+
}
|
|
157
|
+
return obj;
|
|
158
|
+
}
|
|
159
|
+
function _ts_generator(thisArg, body) {
|
|
160
|
+
var f, y, t, _ = {
|
|
161
|
+
label: 0,
|
|
162
|
+
sent: function() {
|
|
163
|
+
if (t[0] & 1) throw t[1];
|
|
164
|
+
return t[1];
|
|
165
|
+
},
|
|
166
|
+
trys: [],
|
|
167
|
+
ops: []
|
|
168
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
169
|
+
return d(g, "next", {
|
|
170
|
+
value: verb(0)
|
|
171
|
+
}), d(g, "throw", {
|
|
172
|
+
value: verb(1)
|
|
173
|
+
}), d(g, "return", {
|
|
174
|
+
value: verb(2)
|
|
175
|
+
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
176
|
+
value: function() {
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
}), g;
|
|
180
|
+
function verb(n) {
|
|
181
|
+
return function(v) {
|
|
182
|
+
return step([
|
|
183
|
+
n,
|
|
184
|
+
v
|
|
185
|
+
]);
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function step(op) {
|
|
189
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
190
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
191
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
192
|
+
if (y = 0, t) op = [
|
|
193
|
+
op[0] & 2,
|
|
194
|
+
t.value
|
|
195
|
+
];
|
|
196
|
+
switch(op[0]){
|
|
197
|
+
case 0:
|
|
198
|
+
case 1:
|
|
199
|
+
t = op;
|
|
200
|
+
break;
|
|
201
|
+
case 4:
|
|
202
|
+
_.label++;
|
|
203
|
+
return {
|
|
204
|
+
value: op[1],
|
|
205
|
+
done: false
|
|
206
|
+
};
|
|
207
|
+
case 5:
|
|
208
|
+
_.label++;
|
|
209
|
+
y = op[1];
|
|
210
|
+
op = [
|
|
211
|
+
0
|
|
212
|
+
];
|
|
213
|
+
continue;
|
|
214
|
+
case 7:
|
|
215
|
+
op = _.ops.pop();
|
|
216
|
+
_.trys.pop();
|
|
217
|
+
continue;
|
|
218
|
+
default:
|
|
219
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
220
|
+
_ = 0;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
224
|
+
_.label = op[1];
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
228
|
+
_.label = t[1];
|
|
229
|
+
t = op;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
if (t && _.label < t[2]) {
|
|
233
|
+
_.label = t[2];
|
|
234
|
+
_.ops.push(op);
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
if (t[2]) _.ops.pop();
|
|
238
|
+
_.trys.pop();
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
op = body.call(thisArg, _);
|
|
242
|
+
} catch (e) {
|
|
243
|
+
op = [
|
|
244
|
+
6,
|
|
245
|
+
e
|
|
246
|
+
];
|
|
247
|
+
y = 0;
|
|
248
|
+
} finally{
|
|
249
|
+
f = t = 0;
|
|
250
|
+
}
|
|
251
|
+
if (op[0] & 5) throw op[1];
|
|
252
|
+
return {
|
|
253
|
+
value: op[0] ? op[1] : void 0,
|
|
254
|
+
done: true
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Injectable wrapper around the Segment Analytics Node SDK.
|
|
260
|
+
*
|
|
261
|
+
* Manages the Analytics client lifecycle, including flushing on module destroy.
|
|
262
|
+
*/ exports.SegmentApi = /*#__PURE__*/ function() {
|
|
263
|
+
function SegmentApi(config) {
|
|
264
|
+
_class_call_check$3(this, SegmentApi);
|
|
265
|
+
_define_property$2(this, "config", void 0);
|
|
266
|
+
_define_property$2(this, "analytics", void 0);
|
|
267
|
+
this.config = config;
|
|
268
|
+
this.analytics = new analyticsNode.Analytics({
|
|
269
|
+
writeKey: this.config.writeKey
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
_create_class$2(SegmentApi, [
|
|
273
|
+
{
|
|
274
|
+
key: "logOnly",
|
|
275
|
+
get: function get() {
|
|
276
|
+
return this.config.logOnly;
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
key: "onModuleDestroy",
|
|
281
|
+
value: function onModuleDestroy() {
|
|
282
|
+
return _async_to_generator(function() {
|
|
283
|
+
return _ts_generator(this, function(_state) {
|
|
284
|
+
switch(_state.label){
|
|
285
|
+
case 0:
|
|
286
|
+
return [
|
|
287
|
+
4,
|
|
288
|
+
this.analytics.closeAndFlush()
|
|
289
|
+
];
|
|
290
|
+
case 1:
|
|
291
|
+
_state.sent();
|
|
292
|
+
return [
|
|
293
|
+
2
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}).call(this);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
]);
|
|
301
|
+
return SegmentApi;
|
|
302
|
+
}();
|
|
303
|
+
exports.SegmentApi = __decorate([
|
|
304
|
+
common.Injectable(),
|
|
305
|
+
__param(0, common.Inject(SegmentServiceConfig))
|
|
306
|
+
], exports.SegmentApi);
|
|
307
|
+
|
|
308
|
+
function _class_call_check$2(instance, Constructor) {
|
|
309
|
+
if (!(instance instanceof Constructor)) {
|
|
310
|
+
throw new TypeError("Cannot call a class as a function");
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function _defineProperties$1(target, props) {
|
|
314
|
+
for(var i = 0; i < props.length; i++){
|
|
315
|
+
var descriptor = props[i];
|
|
316
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
317
|
+
descriptor.configurable = true;
|
|
318
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
319
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function _create_class$1(Constructor, protoProps, staticProps) {
|
|
323
|
+
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
|
324
|
+
return Constructor;
|
|
325
|
+
}
|
|
326
|
+
function _define_property$1(obj, key, value) {
|
|
327
|
+
if (key in obj) {
|
|
328
|
+
Object.defineProperty(obj, key, {
|
|
329
|
+
value: value,
|
|
330
|
+
enumerable: true,
|
|
331
|
+
configurable: true,
|
|
332
|
+
writable: true
|
|
333
|
+
});
|
|
334
|
+
} else {
|
|
335
|
+
obj[key] = value;
|
|
336
|
+
}
|
|
337
|
+
return obj;
|
|
338
|
+
}
|
|
339
|
+
function _object_spread(target) {
|
|
340
|
+
for(var i = 1; i < arguments.length; i++){
|
|
341
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
342
|
+
var ownKeys = Object.keys(source);
|
|
343
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
344
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
345
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
346
|
+
}));
|
|
347
|
+
}
|
|
348
|
+
ownKeys.forEach(function(key) {
|
|
349
|
+
_define_property$1(target, key, source[key]);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
return target;
|
|
353
|
+
}
|
|
354
|
+
function ownKeys(object, enumerableOnly) {
|
|
355
|
+
var keys = Object.keys(object);
|
|
356
|
+
if (Object.getOwnPropertySymbols) {
|
|
357
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
358
|
+
keys.push.apply(keys, symbols);
|
|
359
|
+
}
|
|
360
|
+
return keys;
|
|
361
|
+
}
|
|
362
|
+
function _object_spread_props(target, source) {
|
|
363
|
+
source = source != null ? source : {};
|
|
364
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
365
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
366
|
+
} else {
|
|
367
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
368
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
return target;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* High-level Segment analytics service.
|
|
375
|
+
*
|
|
376
|
+
* Handles track and identify calls, merging application context and
|
|
377
|
+
* supporting log-only mode for development/testing.
|
|
378
|
+
*/ exports.SegmentService = /*#__PURE__*/ function() {
|
|
379
|
+
function SegmentService(segmentApi, config) {
|
|
380
|
+
_class_call_check$2(this, SegmentService);
|
|
381
|
+
_define_property$1(this, "segmentApi", void 0);
|
|
382
|
+
_define_property$1(this, "config", void 0);
|
|
383
|
+
_define_property$1(this, "logger", new common.Logger('SegmentService'));
|
|
384
|
+
this.segmentApi = segmentApi;
|
|
385
|
+
this.config = config;
|
|
386
|
+
}
|
|
387
|
+
_create_class$1(SegmentService, [
|
|
388
|
+
{
|
|
389
|
+
/**
|
|
390
|
+
* Tracks an event for a user. Requires a userId.
|
|
391
|
+
*
|
|
392
|
+
* In log-only mode, events are logged to the console instead of being sent to Segment.
|
|
393
|
+
*
|
|
394
|
+
* @param userId - the user to associate with the event
|
|
395
|
+
* @param event - the track event containing event name, properties, and optional context
|
|
396
|
+
*
|
|
397
|
+
* @throws {Error} When userId is falsy.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* segmentService.track('uid_123', {
|
|
402
|
+
* event: 'Item Purchased',
|
|
403
|
+
* properties: { itemId: 'sku_abc', price: 29.99 }
|
|
404
|
+
* });
|
|
405
|
+
* ```
|
|
406
|
+
*/ key: "track",
|
|
407
|
+
value: function track(userId, event) {
|
|
408
|
+
if (!userId) {
|
|
409
|
+
throw new Error('No userId was provided to track().');
|
|
410
|
+
}
|
|
411
|
+
if (!this.config.logOnly) {
|
|
412
|
+
this.segmentApi.analytics.track({
|
|
413
|
+
userId: userId,
|
|
414
|
+
event: event.event,
|
|
415
|
+
properties: event.properties,
|
|
416
|
+
timestamp: event.timestamp,
|
|
417
|
+
context: _object_spread({}, event.context, this._appContext())
|
|
418
|
+
});
|
|
419
|
+
} else {
|
|
420
|
+
this.logger.debug("Segment (Log Only) - Track: ".concat(userId, " ").concat(event.event));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
/**
|
|
426
|
+
* Tracks an event only if userId is provided. No-op if userId is nullish.
|
|
427
|
+
*
|
|
428
|
+
* Convenience wrapper around {@link track} for cases where the user may not be authenticated.
|
|
429
|
+
*
|
|
430
|
+
* @param userId - the user to associate with the event, or nullish to skip
|
|
431
|
+
* @param event - the track event containing event name, properties, and optional context
|
|
432
|
+
*/ key: "tryTrack",
|
|
433
|
+
value: function tryTrack(userId, event) {
|
|
434
|
+
if (userId) {
|
|
435
|
+
this.track(userId, event);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
/**
|
|
441
|
+
* Identifies a user with optional traits, syncing user properties to Segment.
|
|
442
|
+
*
|
|
443
|
+
* In log-only mode, the identify call is logged to the console instead of being sent.
|
|
444
|
+
*
|
|
445
|
+
* @param params - the identify parameters including userId and optional traits
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* segmentService.identify({
|
|
450
|
+
* userId: 'uid_123',
|
|
451
|
+
* traits: { plan: 'premium', role: 'admin' }
|
|
452
|
+
* });
|
|
453
|
+
* ```
|
|
454
|
+
*/ key: "identify",
|
|
455
|
+
value: function identify(params) {
|
|
456
|
+
if (!this.config.logOnly) {
|
|
457
|
+
this.segmentApi.analytics.identify(_object_spread_props(_object_spread({}, params), {
|
|
458
|
+
context: _object_spread({}, params.context, this._appContext())
|
|
459
|
+
}));
|
|
460
|
+
} else {
|
|
461
|
+
this.logger.debug("Segment (Log Only) - Identify: ".concat(params.userId));
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
key: "_appContext",
|
|
467
|
+
value: function _appContext() {
|
|
468
|
+
var appContext = this.config.appContext;
|
|
469
|
+
var result;
|
|
470
|
+
if (appContext) {
|
|
471
|
+
result = {
|
|
472
|
+
app: appContext
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
]);
|
|
479
|
+
return SegmentService;
|
|
480
|
+
}();
|
|
481
|
+
exports.SegmentService = __decorate([
|
|
482
|
+
common.Injectable(),
|
|
483
|
+
__param(0, common.Inject(exports.SegmentApi)),
|
|
484
|
+
__param(1, common.Inject(SegmentServiceConfig))
|
|
485
|
+
], exports.SegmentService);
|
|
486
|
+
|
|
487
|
+
function _class_call_check$1(instance, Constructor) {
|
|
488
|
+
if (!(instance instanceof Constructor)) {
|
|
489
|
+
throw new TypeError("Cannot call a class as a function");
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Factory that creates a SegmentServiceConfig from environment variables.
|
|
494
|
+
*
|
|
495
|
+
* When a {@link ServerEnvironmentService} is provided and the current environment is a
|
|
496
|
+
* testing environment, `logOnly` is forced to true regardless of the env variable.
|
|
497
|
+
*
|
|
498
|
+
* @param configService - NestJS ConfigService for reading environment variables.
|
|
499
|
+
* @param serverEnvironmentService - Service that identifies the current server environment.
|
|
500
|
+
* @returns A validated {@link SegmentServiceConfig} instance.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* const config = segmentServiceConfigFactory(configService, serverEnvironmentService);
|
|
505
|
+
* ```
|
|
506
|
+
*/ function segmentServiceConfigFactory(configService, serverEnvironmentService) {
|
|
507
|
+
var isTestingEnv = serverEnvironmentService.isTestingEnv;
|
|
508
|
+
var logOnly = isTestingEnv || configService.get('SEGMENT_LOG_ONLY', 'false') === 'true';
|
|
509
|
+
var config = {
|
|
510
|
+
writeKey: configService.get('SEGMENT_WRITE_KEY', ''),
|
|
511
|
+
logOnly: logOnly
|
|
512
|
+
};
|
|
513
|
+
SegmentServiceConfig.assertValidConfig(config);
|
|
514
|
+
return config;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* NestJS module that provides the {@link SegmentService} and its dependencies.
|
|
518
|
+
*
|
|
519
|
+
* Reads `SEGMENT_WRITE_KEY` and `SEGMENT_LOG_ONLY` from environment variables via {@link ConfigModule}.
|
|
520
|
+
* When a {@link ServerEnvironmentService} is available and the environment is a testing environment,
|
|
521
|
+
* `logOnly` is forced to `true`.
|
|
522
|
+
*/ exports.SegmentServiceModule = function SegmentServiceModule() {
|
|
523
|
+
_class_call_check$1(this, SegmentServiceModule);
|
|
524
|
+
};
|
|
525
|
+
exports.SegmentServiceModule = __decorate([
|
|
526
|
+
common.Module({
|
|
527
|
+
imports: [
|
|
528
|
+
config.ConfigModule
|
|
529
|
+
],
|
|
530
|
+
providers: [
|
|
531
|
+
{
|
|
532
|
+
provide: SegmentServiceConfig,
|
|
533
|
+
inject: [
|
|
534
|
+
config.ConfigService,
|
|
535
|
+
nestjs.ServerEnvironmentService
|
|
536
|
+
],
|
|
537
|
+
useFactory: segmentServiceConfigFactory
|
|
538
|
+
},
|
|
539
|
+
exports.SegmentApi,
|
|
540
|
+
exports.SegmentService
|
|
541
|
+
],
|
|
542
|
+
exports: [
|
|
543
|
+
exports.SegmentService
|
|
544
|
+
]
|
|
545
|
+
})
|
|
546
|
+
], exports.SegmentServiceModule);
|
|
547
|
+
|
|
548
|
+
function _class_call_check(instance, Constructor) {
|
|
549
|
+
if (!(instance instanceof Constructor)) {
|
|
550
|
+
throw new TypeError("Cannot call a class as a function");
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function _defineProperties(target, props) {
|
|
554
|
+
for(var i = 0; i < props.length; i++){
|
|
555
|
+
var descriptor = props[i];
|
|
556
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
557
|
+
descriptor.configurable = true;
|
|
558
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
559
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
563
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
564
|
+
return Constructor;
|
|
565
|
+
}
|
|
566
|
+
function _define_property(obj, key, value) {
|
|
567
|
+
if (key in obj) {
|
|
568
|
+
Object.defineProperty(obj, key, {
|
|
569
|
+
value: value,
|
|
570
|
+
enumerable: true,
|
|
571
|
+
configurable: true,
|
|
572
|
+
writable: true
|
|
573
|
+
});
|
|
574
|
+
} else {
|
|
575
|
+
obj[key] = value;
|
|
576
|
+
}
|
|
577
|
+
return obj;
|
|
578
|
+
}
|
|
579
|
+
var ANALYTICS_INTERCEPTOR_METADATA_KEY = 'analyticsevent';
|
|
580
|
+
/**
|
|
581
|
+
* Decorator that marks a controller method for analytics event emission.
|
|
582
|
+
*
|
|
583
|
+
* Used in conjunction with {@link AnalyticsEventInterceptor} to emit events
|
|
584
|
+
* to NestJS EventEmitter2 after the handler completes.
|
|
585
|
+
*
|
|
586
|
+
* @param config - The analytics event configuration specifying the event name and optional data extractor.
|
|
587
|
+
* @returns A method decorator that attaches analytics metadata.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* @EmitAnalyticsEvent({ name: 'User Registered', fn: (result) => ({ userId: result.id }) })
|
|
592
|
+
* @Post('register')
|
|
593
|
+
* async register(@Body() body: RegisterDto) {
|
|
594
|
+
* return this.authService.register(body);
|
|
595
|
+
* }
|
|
596
|
+
* ```
|
|
597
|
+
*/ var EmitAnalyticsEvent = function EmitAnalyticsEvent(config) {
|
|
598
|
+
if (!config.name) {
|
|
599
|
+
throw new Error('Analytics event name was not set properly.');
|
|
600
|
+
}
|
|
601
|
+
return common.SetMetadata(ANALYTICS_INTERCEPTOR_METADATA_KEY, config);
|
|
602
|
+
};
|
|
603
|
+
/**
|
|
604
|
+
* NestJS interceptor that emits analytics events via EventEmitter2
|
|
605
|
+
* for controller methods decorated with {@link EmitAnalyticsEvent}.
|
|
606
|
+
*/ exports.AnalyticsEventInterceptor = /*#__PURE__*/ function() {
|
|
607
|
+
function AnalyticsEventInterceptor(reflector, eventEmitter) {
|
|
608
|
+
_class_call_check(this, AnalyticsEventInterceptor);
|
|
609
|
+
_define_property(this, "reflector", void 0);
|
|
610
|
+
_define_property(this, "eventEmitter", void 0);
|
|
611
|
+
this.reflector = reflector;
|
|
612
|
+
this.eventEmitter = eventEmitter;
|
|
613
|
+
}
|
|
614
|
+
_create_class(AnalyticsEventInterceptor, [
|
|
615
|
+
{
|
|
616
|
+
/**
|
|
617
|
+
* Intercepts the request pipeline, emitting an analytics event after the handler completes
|
|
618
|
+
* if the method is decorated with {@link EmitAnalyticsEvent}.
|
|
619
|
+
*
|
|
620
|
+
* @param context - The NestJS execution context.
|
|
621
|
+
* @param next - The next handler in the pipeline.
|
|
622
|
+
* @returns An observable that emits the handler result after triggering the analytics event.
|
|
623
|
+
*/ key: "intercept",
|
|
624
|
+
value: function intercept(context, next) {
|
|
625
|
+
var _this = this;
|
|
626
|
+
var handler = context.getHandler();
|
|
627
|
+
var config = this.reflector.get(ANALYTICS_INTERCEPTOR_METADATA_KEY, handler);
|
|
628
|
+
return next.handle().pipe(operators.tap(function(result) {
|
|
629
|
+
var eventValues = config.fn ? config.fn(result, context) : undefined;
|
|
630
|
+
if (eventValues != undefined) {
|
|
631
|
+
_this.eventEmitter.emit(config.name, eventValues);
|
|
632
|
+
}
|
|
633
|
+
}));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
]);
|
|
637
|
+
return AnalyticsEventInterceptor;
|
|
638
|
+
}();
|
|
639
|
+
exports.AnalyticsEventInterceptor = __decorate([
|
|
640
|
+
common.Injectable()
|
|
641
|
+
], exports.AnalyticsEventInterceptor);
|
|
642
|
+
|
|
643
|
+
exports.ANALYTICS_INTERCEPTOR_METADATA_KEY = ANALYTICS_INTERCEPTOR_METADATA_KEY;
|
|
644
|
+
exports.EmitAnalyticsEvent = EmitAnalyticsEvent;
|
|
645
|
+
exports.SegmentServiceConfig = SegmentServiceConfig;
|
|
646
|
+
exports.segmentServiceConfigFactory = segmentServiceConfigFactory;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|