@suprsend/web-sdk 0.1.26 → 0.1.29

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.26",
3
+ "version": "0.1.29",
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
@@ -46,3 +46,5 @@ export const os_useragent_map = {
46
46
  Android: "Android",
47
47
  Linux: "Linux",
48
48
  };
49
+
50
+ export const regex = { email: /\S+@\S+\.\S+/ };
package/src/index.js CHANGED
@@ -15,7 +15,7 @@ class SuprSend {
15
15
  var distinct_id = utils.get_cookie(constants.distinct_id);
16
16
  if (!suprSendInstance) {
17
17
  suprSendInstance = {};
18
- this.setCustomConfig(config_keys);
18
+ this._set_custom_config(config_keys);
19
19
  }
20
20
  if (!distinct_id) {
21
21
  distinct_id = utils.uuid();
@@ -25,7 +25,7 @@ class SuprSend {
25
25
  this.user = new User(suprSendInstance);
26
26
  this.web_push = new WebPush(suprSendInstance);
27
27
  this.web_push.update_subscription();
28
- SuprSend.setEnvProperties();
28
+ SuprSend._set_env_properties();
29
29
  if (!initialisedAt) {
30
30
  utils.bulk_call_api();
31
31
  this.track(internal_events.app_launched);
@@ -33,7 +33,7 @@ class SuprSend {
33
33
  initialisedAt = new Date();
34
34
  }
35
35
 
36
- static setEnvProperties() {
36
+ static _set_env_properties() {
37
37
  let device_id = utils.get_local_storage_item(constants.device_id_key);
38
38
  if (!device_id) {
39
39
  device_id = utils.uuid();
@@ -49,7 +49,7 @@ class SuprSend {
49
49
  };
50
50
  }
51
51
 
52
- setCustomConfigProperty(key, value = "", mandatory = false) {
52
+ _set_custom_config_property(key, value = "", mandatory = false) {
53
53
  if (value) {
54
54
  config[key] = value;
55
55
  } else {
@@ -59,12 +59,16 @@ class SuprSend {
59
59
  }
60
60
  }
61
61
 
62
- setCustomConfig(config_keys) {
63
- this.setCustomConfigProperty("env_key", config_keys.env, true);
64
- this.setCustomConfigProperty("signing_key", config_keys.signing_key, true);
65
- this.setCustomConfigProperty("api_url", config_keys?.api_url);
66
- this.setCustomConfigProperty("vapid_key", config_keys?.vapid_key);
67
- 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(
68
72
  "service_worker_file",
69
73
  config_keys?.sw_file_name
70
74
  );
@@ -145,7 +149,14 @@ class SuprSend {
145
149
  this.track(internal_events.purchase_made, props);
146
150
  }
147
151
 
148
- reset() {
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
+ }
149
160
  this.track(internal_events.user_logout);
150
161
  var distinct_id = utils.uuid();
151
162
  utils.set_cookie(constants.distinct_id, distinct_id);
@@ -156,7 +167,7 @@ class SuprSend {
156
167
  utils.remove_local_storage_item(constants.super_properties_key);
157
168
  this.user = new User(suprSendInstance);
158
169
  this.web_push = new WebPush(suprSendInstance);
159
- SuprSend.setEnvProperties();
170
+ SuprSend._set_env_properties();
160
171
  }
161
172
  }
162
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, constants } from "./constants";
3
4
  import { parsePhoneNumber } from "libphonenumber-js";
4
5
 
5
6
  class User {
@@ -17,6 +18,25 @@ class User {
17
18
  });
18
19
  }
19
20
 
21
+ _call_flush_immediately(properties) {
22
+ utils
23
+ .api(constants.api_events_route, {
24
+ env: config.env_key,
25
+ distinct_id: this.instance.distinct_id,
26
+ $insert_id: utils.uuid(),
27
+ $time: utils.epoch_milliseconds(),
28
+ ...properties,
29
+ })
30
+ .then((res) => {
31
+ if (!res.ok) {
32
+ throw new Error("Error in Fetch");
33
+ }
34
+ })
35
+ .catch(() => {
36
+ this._call_indentity(properties);
37
+ });
38
+ }
39
+
20
40
  _allow_special_char_events = (key) => {
21
41
  return (
22
42
  key === "$email" ||
@@ -40,6 +60,14 @@ class User {
40
60
  return obj;
41
61
  }
42
62
 
63
+ _validate_email_and_send(key, email) {
64
+ if (regex.email.test(email)) {
65
+ this.append(key, email);
66
+ } else {
67
+ console.log("Suprsend: Provide valid Email ID");
68
+ }
69
+ }
70
+
43
71
  _validate_mobile_and_send(key, mobile) {
44
72
  try {
45
73
  const mobile_number = parsePhoneNumber(mobile);
@@ -83,11 +111,15 @@ class User {
83
111
  }
84
112
  }
85
113
 
86
- remove(key, value) {
114
+ remove(key, value, config = {}) {
87
115
  const allow_special_tags = this._allow_special_char_events(key);
88
116
  const data = utils.format_props({ key, value, allow_special_tags });
89
117
  if (!utils.is_empty(data)) {
90
- this._call_indentity({ $remove: data });
118
+ if (config?.flush_immediately) {
119
+ this._call_flush_immediately({ $remove: data });
120
+ } else {
121
+ this._call_indentity({ $remove: data });
122
+ }
91
123
  }
92
124
  }
93
125
 
@@ -113,7 +145,7 @@ class User {
113
145
  }
114
146
 
115
147
  add_email(email = "") {
116
- this.append("$email", email);
148
+ this._validate_email_and_send("$email", email);
117
149
  }
118
150
 
119
151
  remove_email(email = "") {
@@ -143,6 +175,18 @@ class User {
143
175
  $pushvendor: "vapid",
144
176
  });
145
177
  }
178
+
179
+ remove_webpush(push = "") {
180
+ this.remove(
181
+ {
182
+ $webpush: push,
183
+ $device_id: this.instance?.env_properties?.$device_id,
184
+ $pushvendor: "vapid",
185
+ },
186
+ null,
187
+ { flush_immediately: true }
188
+ );
189
+ }
146
190
  }
147
191
 
148
192
  export default User;
package/src/utils.js CHANGED
@@ -280,4 +280,5 @@ export default {
280
280
  is_empty,
281
281
  bulk_call_api,
282
282
  is_internal_event,
283
+ api,
283
284
  };