@suprsend/web-sdk 0.1.24 → 0.1.27

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.24",
3
+ "version": "0.1.27",
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,14 +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",
29
- "minify": "^8.0.3",
26
+ "@babel/core": "^7.18.5",
27
+ "@babel/preset-env": "^7.18.2",
28
+ "babel-loader": "^8.2.5",
30
29
  "webpack": "^5.51.1",
31
30
  "webpack-cli": "^4.8.0"
32
31
  },
33
32
  "dependencies": {
34
- "libphonenumber-js": "^1.9.43"
33
+ "libphonenumber-js": "^1.10.7"
35
34
  }
36
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
@@ -1,22 +1,21 @@
1
1
  import utils from "./utils";
2
2
  import config from "./config";
3
3
  import User from "./user";
4
- import ServiceWorker from "./service_worker";
5
- import { constants } from "./constants";
4
+ import WebPush from "./web_push";
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();
@@ -24,13 +23,17 @@ class SuprSend {
24
23
  }
25
24
  suprSendInstance.distinct_id = distinct_id;
26
25
  this.user = new User(suprSendInstance);
27
- this.sw = new ServiceWorker(suprSendInstance);
28
- this.sw.update_subscription();
29
- SuprSend.setEnvProperties();
30
- utils.bulk_call_api();
26
+ this.web_push = new WebPush(suprSendInstance);
27
+ this.web_push.update_subscription();
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,
@@ -101,13 +110,20 @@ class SuprSend {
101
110
  utils.set_cookie(constants.distinct_id, unique_id);
102
111
  suprSendInstance.distinct_id = unique_id;
103
112
  suprSendInstance._user_identified = true;
104
- this.sw.update_subscription();
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,12 @@ class SuprSend {
129
145
  });
130
146
  }
131
147
 
148
+ purchase_made(props) {
149
+ this.track(internal_events.purchase_made, props);
150
+ }
151
+
132
152
  reset() {
153
+ this.track(internal_events.user_logout);
133
154
  var distinct_id = utils.uuid();
134
155
  utils.set_cookie(constants.distinct_id, distinct_id);
135
156
  suprSendInstance = {
@@ -138,8 +159,8 @@ class SuprSend {
138
159
  };
139
160
  utils.remove_local_storage_item(constants.super_properties_key);
140
161
  this.user = new User(suprSendInstance);
141
- this.sw = new ServiceWorker(suprSendInstance);
142
- SuprSend.setEnvProperties();
162
+ this.web_push = new WebPush(suprSendInstance);
163
+ SuprSend._set_env_properties();
143
164
  }
144
165
  }
145
166
 
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 = "") {
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
  };
@@ -1,10 +1,10 @@
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
- class ServiceWorker {
7
+ class WebPush {
8
8
  constructor(instance) {
9
9
  this.instance = instance;
10
10
  this.user = new User(instance);
@@ -45,7 +45,7 @@ class ServiceWorker {
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();
@@ -120,4 +120,4 @@ class ServiceWorker {
120
120
  // };
121
121
  }
122
122
 
123
- export default ServiceWorker;
123
+ export default WebPush;