@suprsend/web-sdk 0.1.21 → 0.1.25
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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cdn_bundle.js +1 -1
- package/dist/cjs_bundle.js +1 -1
- package/package.json +2 -4
- package/serviceworker/serviceworker.js +60 -31
- package/src/index.js +5 -4
- package/src/user.js +3 -0
- package/src/{service_worker.js → web_push.js} +2 -2
- package/serviceworker/serviceworker.min.js +0 -1
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@suprsend/web-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.25",
|
|
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": "
|
|
17
|
+
"license": "MIT",
|
|
19
18
|
"repository": {
|
|
20
19
|
"type": "git",
|
|
21
20
|
"url": "https://github.com/suprsend/suprsend-browser.git"
|
|
@@ -27,7 +26,6 @@
|
|
|
27
26
|
"@babel/core": "^7.15.0",
|
|
28
27
|
"@babel/preset-env": "^7.15.0",
|
|
29
28
|
"babel-loader": "^8.2.2",
|
|
30
|
-
"minify": "^8.0.3",
|
|
31
29
|
"webpack": "^5.51.1",
|
|
32
30
|
"webpack-cli": "^4.8.0"
|
|
33
31
|
},
|
|
@@ -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
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
|
-
|
|
53
|
+
var url_fields = ["image", "icon", "badge"];
|
|
26
54
|
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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
|
|
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: {
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
88
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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/index.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
|
|
4
|
+
import WebPush from "./web_push";
|
|
5
5
|
import { constants } from "./constants";
|
|
6
6
|
import { SSConfigurationError } from "./errors";
|
|
7
7
|
|
|
@@ -24,8 +24,8 @@ class SuprSend {
|
|
|
24
24
|
}
|
|
25
25
|
suprSendInstance.distinct_id = distinct_id;
|
|
26
26
|
this.user = new User(suprSendInstance);
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
27
|
+
this.web_push = new WebPush(suprSendInstance);
|
|
28
|
+
this.web_push.update_subscription();
|
|
29
29
|
SuprSend.setEnvProperties();
|
|
30
30
|
utils.bulk_call_api();
|
|
31
31
|
}
|
|
@@ -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.web_push.update_subscription();
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
|
|
@@ -137,7 +138,7 @@ class SuprSend {
|
|
|
137
138
|
};
|
|
138
139
|
utils.remove_local_storage_item(constants.super_properties_key);
|
|
139
140
|
this.user = new User(suprSendInstance);
|
|
140
|
-
this.
|
|
141
|
+
this.web_push = new WebPush(suprSendInstance);
|
|
141
142
|
SuprSend.setEnvProperties();
|
|
142
143
|
}
|
|
143
144
|
}
|
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
|
}
|
|
@@ -4,7 +4,7 @@ import User from "./user";
|
|
|
4
4
|
import { init_at } from "./index";
|
|
5
5
|
|
|
6
6
|
var notification_timer;
|
|
7
|
-
class
|
|
7
|
+
class WebPush {
|
|
8
8
|
constructor(instance) {
|
|
9
9
|
this.instance = instance;
|
|
10
10
|
this.user = new User(instance);
|
|
@@ -120,4 +120,4 @@ class ServiceWorker {
|
|
|
120
120
|
// };
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
export default
|
|
123
|
+
export default WebPush;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var suprsend_config={api_url:"https://hub.suprsend.com",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)}));
|