-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathEvent.js
127 lines (118 loc) · 4.97 KB
/
Event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as L from "./Logger.js";
import * as Validate from "./Validators.js";
class Event {
#state;
constructor(state) {
this.#state = state;
}
/**
* Records an event.
* Event will be saved to the internal queue and will be sent to the server with the next trigger.
*
* @param {string} eventName - Name of the event (This will be displayed on the dashboard)
* @param {Segmentation} segmentation - Extra information to send with your event as key/value pairs
* @param {number} eventCount - Indicates how many times this event has happened (Default is 1)
* @param {number} eventSum - A numerical value that is attached to this event (Will be summed up on the dashboard for all events with the same name)
* @return {void}
*/
recordEvent(eventName, segmentation, eventCount, eventSum) {
if (!this.#state.isInitialized) {
L.w("recordEvent, SDK must be initialized before calling 'recordEvent'");
return;
}
L.i(`recordEvent, called with eventName: [${eventName}], segmentation: [${JSON.stringify(segmentation)}], eventCount: [${eventCount}], eventSum: [${eventSum}]`);
const areParamsValid = Validate.areEventParametersValid("recordEvent", eventName, segmentation, eventCount, eventSum);
if (!areParamsValid) {
return;
}
// At this point all parameters should be valid (eventName should exist but other parameters are optional)
const args = {};
args.n = eventName; // mandatory
args.c = eventCount || 1; // default is 1
args.s = eventSum || 0; // default is 0
if (segmentation) { // optional
args.g = [];
for (const key in segmentation) {
args.g.push(key);
args.g.push(segmentation[key]);
}
}
this.#state.CountlyReactNative.recordEvent(args);
}
/**
*
* Starts a Timed Event
* If 'endEvent' is not called (with the same event name) no event will be recorded.
*
* @param {string} eventName - name of the event
* @return {void}
*/
startEvent(eventName) {
if (!this.#state.isInitialized) {
L.w("startEvent, SDK must be initialized before calling 'startEvent'");
return;
}
L.i(`startEvent, called with eventName: [${eventName}]`);
const areParamsValid = Validate.areEventParametersValid("startEvent", eventName, null, null, null);
if (!areParamsValid) {
return;
}
this.#state.CountlyReactNative.startEvent([eventName]);
}
/**
*
* Cancels a Timed Event if it is started.
*
* @param {string} eventName - name of the event
* @return {void}
*/
cancelEvent(eventName) {
if (!this.#state.isInitialized) {
L.w("cancelEvent, SDK must be initialized before calling 'cancelEvent'");
return;
}
L.i(`cancelEvent, called with eventName: [${eventName}]`);
const areParamsValid = Validate.areEventParametersValid("cancelEvent", eventName, null, null, null);
if (!areParamsValid) {
return;
}
this.#state.CountlyReactNative.cancelEvent([eventName]);
}
/**
*
* Ends a Timed Event if it is started.
* Should be called after startEvent.
* This will behave like recordEvent.
*
* @param {string} eventName - Name of the event (This will be displayed on the dashboard)
* @param {Segmentation} segmentation - Extra information to send with your event as key/value pairs
* @param {number} eventCount - Indicates how many times this event has happened (Default is 1)
* @param {number} eventSum - A numerical value that is attached to this event (Will be summed up on the dashboard for all events with the same name)
* @return {void} void
*/
endEvent(eventName, segmentation, eventCount, eventSum) {
if (!this.#state.isInitialized) {
L.w("endEvent, SDK must be initialized before calling 'endEvent'");
return;
}
L.i(`endEvent, called with eventName: [${eventName}], segmentation: [${JSON.stringify(segmentation)}], eventCount: [${eventCount}], eventSum: [${eventSum}]`);
const validParameters = Validate.areEventParametersValid("endEvent", eventName, segmentation, eventCount, eventSum);
if (!validParameters) {
return;
}
// At this point all parameters should be valid (eventName should exist but other parameters are optional)
const args = {};
args.n = eventName; // mandatory
args.c = eventCount || 1; // default is 1
args.s = eventSum || 0; // default is 0
if (segmentation) { // optional
args.g = [];
for (const key in segmentation) {
args.g.push(key);
args.g.push(segmentation[key]);
}
}
this.#state.CountlyReactNative.endEvent(args);
}
}
export default Event;