@suprsend/web-sdk 0.1.20 → 0.1.24

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,12 +1,11 @@
1
1
  {
2
2
  "name": "@suprsend/web-sdk",
3
- "version": "0.1.20",
3
+ "version": "0.1.24",
4
4
  "description": "This is sdk used to integrate suprsend functionality in javascript applications",
5
5
  "main": "dist/cjs_bundle.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1",
8
8
  "build": "rm -rf dist && webpack --env module_type=commonjs --env filename=cjs_bundle.js && webpack --env module_type=window --env filename=cdn_bundle.js",
9
- "minify-sw": "touch ./serviceworker/serviceworker.min.js && rm ./serviceworker/serviceworker.min.js && minify ./serviceworker/serviceworker.js > ./serviceworker/serviceworker.min.js",
10
9
  "publish-sdk": "npm run build && npm publish"
11
10
  },
12
11
  "keywords": [
@@ -15,7 +14,7 @@
15
14
  "suprsend-browser-sdk"
16
15
  ],
17
16
  "author": "Sivaram Katta",
18
- "license": "ISC",
17
+ "license": "MIT",
19
18
  "repository": {
20
19
  "type": "git",
21
20
  "url": "https://github.com/suprsend/suprsend-browser.git"
@@ -1,7 +1,35 @@
1
+ function uuid() {
2
+ var dt = new Date().getTime();
3
+ var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
4
+ /[xy]/g,
5
+ function (c) {
6
+ var r = (dt + Math.random() * 16) % 16 | 0;
7
+ dt = Math.floor(dt / 16);
8
+ return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
9
+ }
10
+ );
11
+ return uuid;
12
+ }
13
+
14
+ function epoch_milliseconds() {
15
+ return Math.round(Date.now());
16
+ }
17
+
18
+ function safe_get(cb, default_value) {
19
+ var resp;
20
+ try {
21
+ resp = cb();
22
+ } catch (err) {
23
+ resp = default_value;
24
+ }
25
+ return resp;
26
+ }
27
+
1
28
  var suprsend_config = {
2
- api_url: "https://collector-staging.suprsend.workers.dev",
29
+ api_url: "https://hub.suprsend.com",
3
30
  imgkit_root: "https://ik.imagekit.io/l0quatz6utm/",
4
31
  api_events_route: "event/",
32
+ workspace_key: "",
5
33
  };
6
34
 
7
35
  var valid_notification_params = [
@@ -22,20 +50,17 @@ var valid_notification_params = [
22
50
  "actions",
23
51
  ];
24
52
 
25
- const url_fields = ["image", "icon", "badge"];
53
+ var url_fields = ["image", "icon", "badge"];
26
54
 
27
- function safe_get(cb, default_value) {
28
- let resp;
29
- try {
30
- resp = cb();
31
- } catch (err) {
32
- resp = default_value;
55
+ function init_workspace(key, url) {
56
+ suprsend_config.workspace_key = key;
57
+ if (url) {
58
+ suprsend_config.api_url = url;
33
59
  }
34
- return resp;
35
60
  }
36
61
 
37
62
  function validate_notification(notification_obj) {
38
- let validated_notification_obj = {};
63
+ var validated_notification_obj = {};
39
64
  for (var item in notification_obj) {
40
65
  if (valid_notification_params.includes(item)) {
41
66
  if (url_fields.includes(item)) {
@@ -53,26 +78,38 @@ function validate_notification(notification_obj) {
53
78
  return validated_notification_obj;
54
79
  }
55
80
 
56
- function track_data(body, method = "post") {
81
+ function call_ss_api(body, method = "post") {
82
+ var requested_date = new Date().toGMTString();
83
+ var authorization = suprsend_config.workspace_key + ":";
57
84
  return fetch(
58
85
  `${suprsend_config.api_url}/${suprsend_config.api_events_route}`,
59
86
  {
60
87
  method: method,
61
88
  body: JSON.stringify(body),
62
- headers: { "Content-Type": "application/json" },
89
+ headers: {
90
+ "Content-Type": "application/json",
91
+ Authorization: authorization,
92
+ "x-amz-date": requested_date,
93
+ },
63
94
  }
64
95
  );
65
96
  }
66
97
 
98
+ function send_notification_event(event_name, event_properties) {
99
+ call_ss_api({
100
+ event: event_name,
101
+ env: suprsend_config.workspace_key,
102
+ $insert_id: uuid(),
103
+ $time: epoch_milliseconds(),
104
+ properties: event_properties,
105
+ });
106
+ }
107
+
67
108
  self.addEventListener("push", function (e) {
68
109
  var notification = e.data.json();
69
- const validated_notification = validate_notification(notification);
70
- console.log("Received notification", validated_notification);
71
- track_data({
72
- event: "$notification_delivered",
73
- properties: {
74
- id: safe_get(() => validated_notification.data.notification_id),
75
- },
110
+ var validated_notification = validate_notification(notification);
111
+ send_notification_event("$notification_delivered", {
112
+ id: safe_get(() => validated_notification.data.notification_id),
76
113
  });
77
114
  e.waitUntil(
78
115
  self.registration.showNotification(
@@ -84,25 +121,17 @@ self.addEventListener("push", function (e) {
84
121
 
85
122
  self.addEventListener("notificationclose", function (e) {
86
123
  var notification = e.notification;
87
- console.log("Closed notification", notification);
88
- track_data({
89
- event: "$notification_dismiss",
90
- properties: {
91
- id: safe_get(() => notification.data.notification_id),
92
- },
124
+ send_notification_event("$notification_dismiss", {
125
+ id: safe_get(() => notification.data.notification_id),
93
126
  });
94
127
  });
95
128
 
96
129
  self.addEventListener("notificationclick", function (e) {
97
130
  e.notification.close();
98
131
  var notification = e.notification;
99
- console.log("Clicked notification", notification);
100
- track_data({
101
- event: "$notification_clicked",
102
- properties: {
103
- id: safe_get(() => notification.data.notification_id),
104
- label_id: e.action,
105
- },
132
+ send_notification_event("$notification_clicked", {
133
+ id: safe_get(() => notification.data.notification_id),
134
+ label_id: e.action,
106
135
  });
107
136
  var launch_url_obj = safe_get(() => notification.data.launch_urls);
108
137
  var redirect_url = "/";
package/src/config.js CHANGED
@@ -4,6 +4,7 @@ const config = {
4
4
  api_url: "https://hub.suprsend.com",
5
5
  sdk_version: package_data.version,
6
6
  batch_size: 20,
7
+ flush_interval: 3000,
7
8
  service_worker_file: "serviceworker.js",
8
9
  sw_delay: 5000, //in ms,
9
10
  };
package/src/index.js CHANGED
@@ -27,7 +27,7 @@ class SuprSend {
27
27
  this.sw = new ServiceWorker(suprSendInstance);
28
28
  this.sw.update_subscription();
29
29
  SuprSend.setEnvProperties();
30
- utils.schedule_flush();
30
+ utils.bulk_call_api();
31
31
  }
32
32
 
33
33
  static setEnvProperties() {
@@ -101,6 +101,7 @@ class SuprSend {
101
101
  utils.set_cookie(constants.distinct_id, unique_id);
102
102
  suprSendInstance.distinct_id = unique_id;
103
103
  suprSendInstance._user_identified = true;
104
+ this.sw.update_subscription();
104
105
  }
105
106
  }
106
107
 
package/src/user.js CHANGED
@@ -11,6 +11,8 @@ class User {
11
11
  utils.batch_or_call({
12
12
  env: config.env_key,
13
13
  distinct_id: this.instance.distinct_id,
14
+ $insert_id: utils.uuid(),
15
+ $time: utils.epoch_milliseconds(),
14
16
  ...properties,
15
17
  });
16
18
  }
@@ -138,6 +140,7 @@ class User {
138
140
  this.append({
139
141
  $webpush: push,
140
142
  $device_id: this.instance?.env_properties?.$device_id,
143
+ $pushvendor: "vapid",
141
144
  });
142
145
  }
143
146
  }
package/src/utils.js CHANGED
@@ -174,7 +174,7 @@ function bulk_call_api() {
174
174
  /*
175
175
  schedule the flush in some time future
176
176
  */
177
- function schedule_flush(delay = 5000) {
177
+ function schedule_flush(delay = config.flush_interval) {
178
178
  setTimeout(() => {
179
179
  bulk_call_api();
180
180
  }, delay);
@@ -271,4 +271,5 @@ export default {
271
271
  batch_or_call,
272
272
  has_special_char,
273
273
  is_empty,
274
+ bulk_call_api,
274
275
  };
@@ -1 +0,0 @@
1
- var suprsend_config={api_url:"https://collector-staging.suprsend.workers.dev",imgkit_root:"https://ik.imagekit.io/l0quatz6utm/",api_events_route:"event/"},valid_notification_params=["title","body","icon","image","badge","vibrate","sound","dir","tag","data","requireInteraction","renotify","silent","timestamp","actions"];const url_fields=["image","icon","badge"];function safe_get(i,t){let n;try{n=i()}catch(i){n=t}return n}function validate_notification(i){let t={};for(var n in i)valid_notification_params.includes(n)&&(url_fields.includes(n)?t[n]=`${suprsend_config.imgkit_root}${i[n]}`:t[n]=i[n]);return t.actions instanceof Array||delete t.actions,t}function track_data(i,t="post"){return fetch(`${suprsend_config.api_url}/${suprsend_config.api_events_route}`,{method:t,body:JSON.stringify(i),headers:{"Content-Type":"application/json"}})}self.addEventListener("push",(function(i){const t=validate_notification(i.data.json());console.log("Received notification",t),track_data({event:"$notification_delivered",properties:{id:safe_get((()=>t.data.notification_id))}}),i.waitUntil(self.registration.showNotification(t.title||"",t))})),self.addEventListener("notificationclose",(function(i){var t=i.notification;console.log("Closed notification",t),track_data({event:"$notification_dismiss",properties:{id:safe_get((()=>t.data.notification_id))}})})),self.addEventListener("notificationclick",(function(i){i.notification.close();var t=i.notification;console.log("Clicked notification",t),track_data({event:"$notification_clicked",properties:{id:safe_get((()=>t.data.notification_id)),label_id:i.action}});var n=safe_get((()=>t.data.launch_urls)),e="/";n?i.action&&n[i.action]?e=n[i.action]:n.default&&(e=n.default):e="/",clients.openWindow(e)}));