@suprsend/web-sdk 0.1.25 → 0.1.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/web-sdk",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "This is sdk used to integrate suprsend functionality in javascript applications",
5
5
  "main": "dist/cjs_bundle.js",
6
6
  "scripts": {
package/src/constants.js CHANGED
@@ -7,6 +7,13 @@ export const constants = {
7
7
  device_id_key: "_suprsend_device_id",
8
8
  };
9
9
 
10
+ export const internal_events = {
11
+ app_launched: "$app_launched",
12
+ user_login: "$user_login",
13
+ user_logout: "$user_logout",
14
+ purchase_made: "$purchase_made",
15
+ };
16
+
10
17
  export const browser_useragent_map = {
11
18
  Edge: ["Edge"],
12
19
  "Opera Mini": ["Opera Mini"],
package/src/index.js CHANGED
@@ -2,17 +2,16 @@ import utils from "./utils";
2
2
  import config from "./config";
3
3
  import User from "./user";
4
4
  import WebPush from "./web_push";
5
- import { constants } from "./constants";
5
+ import { constants, internal_events } from "./constants";
6
6
  import { SSConfigurationError } from "./errors";
7
7
 
8
8
  var suprSendInstance;
9
- export var init_at;
9
+ export var initialisedAt;
10
10
 
11
11
  class SuprSend {
12
12
  init(ENV_API_KEY, SIGNING_KEY, config_keys = {}) {
13
13
  config_keys.env = ENV_API_KEY;
14
14
  config_keys.signing_key = SIGNING_KEY;
15
- init_at = new Date();
16
15
  var distinct_id = utils.get_cookie(constants.distinct_id);
17
16
  if (!suprSendInstance) {
18
17
  suprSendInstance = {};
@@ -27,7 +26,11 @@ class SuprSend {
27
26
  this.web_push = new WebPush(suprSendInstance);
28
27
  this.web_push.update_subscription();
29
28
  SuprSend.setEnvProperties();
30
- utils.bulk_call_api();
29
+ if (!initialisedAt) {
30
+ utils.bulk_call_api();
31
+ this.track(internal_events.app_launched);
32
+ }
33
+ initialisedAt = new Date();
31
34
  }
32
35
 
33
36
  static setEnvProperties() {
@@ -93,6 +96,8 @@ class SuprSend {
93
96
  utils.batch_or_call({
94
97
  env: config.env_key,
95
98
  event: "$identify",
99
+ $insert_id: utils.uuid(),
100
+ $time: utils.epoch_milliseconds(),
96
101
  properties: {
97
102
  $identified_id: unique_id,
98
103
  $anon_id: suprSendInstance.distinct_id,
@@ -102,12 +107,19 @@ class SuprSend {
102
107
  suprSendInstance.distinct_id = unique_id;
103
108
  suprSendInstance._user_identified = true;
104
109
  this.web_push.update_subscription();
110
+ this.track(internal_events.user_login);
105
111
  }
106
112
  }
107
113
 
108
114
  track(event, props = {}) {
109
115
  if (event === undefined) {
110
116
  return;
117
+ } else if (
118
+ !utils.is_internal_event(event) &&
119
+ utils.has_special_char(event)
120
+ ) {
121
+ console.log("Suprsend: key cannot start with $ or ss_");
122
+ return;
111
123
  }
112
124
  const super_props = utils.get_parsed_local_store_data(
113
125
  constants.super_properties_key
@@ -129,7 +141,12 @@ class SuprSend {
129
141
  });
130
142
  }
131
143
 
144
+ purchase_made(props) {
145
+ this.track(internal_events.purchase_made, props);
146
+ }
147
+
132
148
  reset() {
149
+ this.track(internal_events.user_logout);
133
150
  var distinct_id = utils.uuid();
134
151
  utils.set_cookie(constants.distinct_id, distinct_id);
135
152
  suprSendInstance = {
package/src/utils.js CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  browser_version_useragent_map,
4
4
  os_useragent_map,
5
5
  constants,
6
+ internal_events,
6
7
  } from "./constants";
7
8
  import config from "./config";
8
9
  import create_signature from "./encryption";
@@ -207,6 +208,7 @@ function format_props({ key, value, allow_special_tags = false }) {
207
208
  for (let item in key) {
208
209
  if (key[item] !== undefined) {
209
210
  if (!allow_special_tags && has_special_char(item)) {
211
+ console.log("Suprsend: key cannot start with $ or ss_");
210
212
  continue;
211
213
  }
212
214
  formatted_data[String(item)] = key[item];
@@ -239,6 +241,11 @@ const has_special_char = (str) => {
239
241
  return str.startsWith("$") || str?.toLowerCase()?.startsWith("ss_");
240
242
  };
241
243
 
244
+ const is_internal_event = (event) => {
245
+ const internal_events_list = Object.values(internal_events);
246
+ return internal_events_list.includes(event);
247
+ };
248
+
242
249
  const is_empty = (value) => {
243
250
  if (Array.isArray(value)) {
244
251
  return value.length === 0;
@@ -272,4 +279,5 @@ export default {
272
279
  has_special_char,
273
280
  is_empty,
274
281
  bulk_call_api,
282
+ is_internal_event,
275
283
  };
package/src/web_push.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import utils from "./utils";
2
2
  import config from "./config";
3
3
  import User from "./user";
4
- import { init_at } from "./index";
4
+ import { initialisedAt } from "./index";
5
5
 
6
6
  var notification_timer;
7
7
  class WebPush {
@@ -45,7 +45,7 @@ class WebPush {
45
45
  // this method make sure there is a given delay, as calling notification permission just after load is not good UX practice
46
46
  _subscribe_with_delay() {
47
47
  const now = new Date();
48
- const delay = now - init_at;
48
+ const delay = now - initialisedAt;
49
49
  const has_delay = delay >= config.sw_delay;
50
50
  if (has_delay) {
51
51
  this._register_sw();