@suprsend/web-sdk 0.1.25 → 0.1.28

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.
@@ -0,0 +1 @@
1
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
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.28",
4
4
  "description": "This is sdk used to integrate suprsend functionality in javascript applications",
5
5
  "main": "dist/cjs_bundle.js",
6
6
  "scripts": {
@@ -23,13 +23,13 @@
23
23
  "access": "public"
24
24
  },
25
25
  "devDependencies": {
26
- "@babel/core": "^7.15.0",
27
- "@babel/preset-env": "^7.15.0",
28
- "babel-loader": "^8.2.2",
26
+ "@babel/core": "^7.18.5",
27
+ "@babel/preset-env": "^7.18.2",
28
+ "babel-loader": "^8.2.5",
29
29
  "webpack": "^5.51.1",
30
30
  "webpack-cli": "^4.8.0"
31
31
  },
32
32
  "dependencies": {
33
- "libphonenumber-js": "^1.9.43"
33
+ "libphonenumber-js": "^1.10.7"
34
34
  }
35
35
  }
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"],
@@ -39,3 +46,5 @@ export const os_useragent_map = {
39
46
  Android: "Android",
40
47
  Linux: "Linux",
41
48
  };
49
+
50
+ export const regex = { email: /\S+@\S+\.\S+/ };
package/src/index.js CHANGED
@@ -2,21 +2,20 @@ 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 = {};
19
- this.setCustomConfig(config_keys);
18
+ this._set_custom_config(config_keys);
20
19
  }
21
20
  if (!distinct_id) {
22
21
  distinct_id = utils.uuid();
@@ -26,11 +25,15 @@ class SuprSend {
26
25
  this.user = new User(suprSendInstance);
27
26
  this.web_push = new WebPush(suprSendInstance);
28
27
  this.web_push.update_subscription();
29
- SuprSend.setEnvProperties();
30
- utils.bulk_call_api();
28
+ SuprSend._set_env_properties();
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
- static setEnvProperties() {
36
+ static _set_env_properties() {
34
37
  let device_id = utils.get_local_storage_item(constants.device_id_key);
35
38
  if (!device_id) {
36
39
  device_id = utils.uuid();
@@ -46,7 +49,7 @@ class SuprSend {
46
49
  };
47
50
  }
48
51
 
49
- setCustomConfigProperty(key, value = "", mandatory = false) {
52
+ _set_custom_config_property(key, value = "", mandatory = false) {
50
53
  if (value) {
51
54
  config[key] = value;
52
55
  } else {
@@ -56,12 +59,16 @@ class SuprSend {
56
59
  }
57
60
  }
58
61
 
59
- setCustomConfig(config_keys) {
60
- this.setCustomConfigProperty("env_key", config_keys.env, true);
61
- this.setCustomConfigProperty("signing_key", config_keys.signing_key, true);
62
- this.setCustomConfigProperty("api_url", config_keys?.api_url);
63
- this.setCustomConfigProperty("vapid_key", config_keys?.vapid_key);
64
- this.setCustomConfigProperty(
62
+ _set_custom_config(config_keys) {
63
+ this._set_custom_config_property("env_key", config_keys.env, true);
64
+ this._set_custom_config_property(
65
+ "signing_key",
66
+ config_keys.signing_key,
67
+ true
68
+ );
69
+ this._set_custom_config_property("api_url", config_keys?.api_url);
70
+ this._set_custom_config_property("vapid_key", config_keys?.vapid_key);
71
+ this._set_custom_config_property(
65
72
  "service_worker_file",
66
73
  config_keys?.sw_file_name
67
74
  );
@@ -93,6 +100,8 @@ class SuprSend {
93
100
  utils.batch_or_call({
94
101
  env: config.env_key,
95
102
  event: "$identify",
103
+ $insert_id: utils.uuid(),
104
+ $time: utils.epoch_milliseconds(),
96
105
  properties: {
97
106
  $identified_id: unique_id,
98
107
  $anon_id: suprSendInstance.distinct_id,
@@ -102,12 +111,19 @@ class SuprSend {
102
111
  suprSendInstance.distinct_id = unique_id;
103
112
  suprSendInstance._user_identified = true;
104
113
  this.web_push.update_subscription();
114
+ this.track(internal_events.user_login);
105
115
  }
106
116
  }
107
117
 
108
118
  track(event, props = {}) {
109
119
  if (event === undefined) {
110
120
  return;
121
+ } else if (
122
+ !utils.is_internal_event(event) &&
123
+ utils.has_special_char(event)
124
+ ) {
125
+ console.log("Suprsend: key cannot start with $ or ss_");
126
+ return;
111
127
  }
112
128
  const super_props = utils.get_parsed_local_store_data(
113
129
  constants.super_properties_key
@@ -129,7 +145,19 @@ class SuprSend {
129
145
  });
130
146
  }
131
147
 
132
- reset() {
148
+ purchase_made(props) {
149
+ this.track(internal_events.purchase_made, props);
150
+ }
151
+
152
+ async reset(options = { unsubscribe_push: true }) {
153
+ // unsubscribe push
154
+ if (options?.unsubscribe_push) {
155
+ const subscription = await this.web_push._get_subscription();
156
+ if (subscription) {
157
+ this.user.remove_webpush(subscription);
158
+ }
159
+ }
160
+ this.track(internal_events.user_logout);
133
161
  var distinct_id = utils.uuid();
134
162
  utils.set_cookie(constants.distinct_id, distinct_id);
135
163
  suprSendInstance = {
@@ -139,7 +167,7 @@ class SuprSend {
139
167
  utils.remove_local_storage_item(constants.super_properties_key);
140
168
  this.user = new User(suprSendInstance);
141
169
  this.web_push = new WebPush(suprSendInstance);
142
- SuprSend.setEnvProperties();
170
+ SuprSend._set_env_properties();
143
171
  }
144
172
  }
145
173
 
package/src/user.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import utils from "./utils";
2
2
  import config from "./config";
3
+ import { regex } from "./constants";
3
4
  import { parsePhoneNumber } from "libphonenumber-js";
4
5
 
5
6
  class User {
@@ -40,6 +41,14 @@ class User {
40
41
  return obj;
41
42
  }
42
43
 
44
+ _validate_email_and_send(key, email) {
45
+ if (regex.email.test(email)) {
46
+ this.append(key, email);
47
+ } else {
48
+ console.log("Suprsend: Provide valid Email ID");
49
+ }
50
+ }
51
+
43
52
  _validate_mobile_and_send(key, mobile) {
44
53
  try {
45
54
  const mobile_number = parsePhoneNumber(mobile);
@@ -113,7 +122,7 @@ class User {
113
122
  }
114
123
 
115
124
  add_email(email = "") {
116
- this.append("$email", email);
125
+ this._validate_email_and_send("$email", email);
117
126
  }
118
127
 
119
128
  remove_email(email = "") {
@@ -143,6 +152,14 @@ class User {
143
152
  $pushvendor: "vapid",
144
153
  });
145
154
  }
155
+
156
+ remove_webpush(push = "") {
157
+ this.remove({
158
+ $webpush: push,
159
+ $device_id: this.instance?.env_properties?.$device_id,
160
+ $pushvendor: "vapid",
161
+ });
162
+ }
146
163
  }
147
164
 
148
165
  export default User;
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();