@suprsend/node-sdk 1.1.0 → 1.1.1

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/README.md CHANGED
@@ -3,109 +3,20 @@ This package can be included in a node project to easily integrate with `Suprsen
3
3
 
4
4
  Refer full documentation [here](https://docs.suprsend.com/docs/node)
5
5
 
6
- We're working towards creating SDK in other languages as well.
7
-
8
- ### Suprsend SDKs available in following languages
9
- * node (`suprsend-node-sdk`)
10
- * python3 >= 3.7 (`suprsend-py-sdk`)
11
-
12
6
  ### Installation
13
7
  `suprsend-node-sdk` is available as npm package. You can install using npm or yarn.
14
-
15
- Using npm:
16
8
  ```bash
17
9
  npm install @suprsend/node-sdk
18
10
  ```
19
- Using yarn:
20
- ```bash
21
- yarn add @suprsend/node-sdk
22
- ```
23
11
 
24
- ### Usage
12
+ ### Initialization
25
13
  Initialize the Suprsend SDK
26
14
  ```node
27
- const Suprsend = require("@suprsend/node-sdk");
15
+ const {Suprsend} = require("@suprsend/node-sdk");
28
16
 
29
17
  // Initialize SDK
30
- const supr_client = new Suprsend("env_key", "env_secret");
31
- ```
32
-
33
- Following example shows a sample request for triggering a workflow.
34
- It triggers a notification to a user with id: `distinct_id`,
35
- email: `user@example.com` & androidpush-token: `__android_push_token__`
36
- using template `purchase-made` and notification_category `system`
37
-
38
- ```node
39
- // Prepare Workflow body
40
- const workflow_body = {
41
- "name": "Purchase Workflow",
42
- "template": "purchase-made",
43
- "notification_category": "system",
44
- "delay": "15m",
45
- "users": [
46
- {
47
- "distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
48
- "$email": ["user@example.com"],
49
- "$androidpush": ["__android_push_token__"],
50
- }
51
- ],
52
- "data": {
53
- "template": {
54
- "first_name": "User",
55
- "spend_amount": "$10"
56
- },
57
- }
58
- }
59
-
60
- // Trigger workflow
61
- const response = supr_client.trigger_workflow(workflow_body); // returns promise
62
- response.then((res) => console.log("response", res));
63
- ```
64
-
65
- When you call `supr_client.trigger_workflow`, the SDK internally makes an HTTP call to SuprSend
66
- Platform to register this request, and you'll receive a promise which resolve to response indicating
67
- the acceptance status.
68
-
69
- Note: The actual processing/execution of workflow happens asynchronously.
70
-
71
- ```node
72
- // If the call succeeds, response will looks like:
73
- {
74
- "success": true,
75
- "status_code": 202,
76
- "message": "Accepted",
77
- }
78
-
79
- // In case the call fails. You will receive a response with success=false
80
- {
81
- "success": false,
82
- "status": 400,
83
- "message": "error message",
84
- }
18
+ const supr_client = new Suprsend("workspace_key", "workspace_secret");
85
19
  ```
86
20
 
87
- ### Add attachments
88
-
89
- To add one or more Attachments to a Notification (viz. Email, Whatsapp),
90
- call `supr_client.add_attachment(...)` for each file.
91
- Ensure that file_path is proper, otherwise it will raise error.
92
- ```node
93
- // this snippet can be used to add attachment to workflow_body.
94
- const file_path = "/home/user/billing.pdf"
95
- supr_client.add_attachment(workflow_body, file_path);
96
- ```
97
-
98
- #### Attachment structure
99
- The `add_attachment(...)` call appends below structure to `data->'$attachments'`
100
-
101
- ```json
102
- {
103
- "filename": "billing.pdf",
104
- "contentType": "application/pdf",
105
- "data": "Q29uZ3JhdHVsYXRpb25zLCB5b3UgY2FuIGJhc2U2NCBkZWNvZGUh",
106
- }
107
- ```
108
- Where
109
- * `filename` - name of file.
110
- * `contentType` - MIME-type of file content.
111
- * `data` - base64-encoded content of file.
21
+ ### License
22
+ MIT © [https://github.com/suprsend](https://github.com/https://github.com/suprsend)
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+
12
+ var LANG_CODE_NAME_MAPPING = {
13
+ aa: "Afar",
14
+ ab: "Abkhazian",
15
+ ae: "Avestan",
16
+ af: "Afrikaans",
17
+ ak: "Akan",
18
+ am: "Amharic",
19
+ an: "Aragonese",
20
+ ar: "Arabic",
21
+ as: "Assamese",
22
+ av: "Avaric",
23
+ ay: "Aymara",
24
+ az: "Azerbaijani",
25
+ ba: "Bashkir",
26
+ be: "Belarusian",
27
+ bg: "Bulgarian",
28
+ bi: "Bislama",
29
+ bm: "Bambara",
30
+ bn: "Bengali",
31
+ bo: "Tibetan",
32
+ br: "Breton",
33
+ bs: "Bosnian",
34
+ ca: "Catalan; Valencian",
35
+ ce: "Chechen",
36
+ ch: "Chamorro",
37
+ co: "Corsican",
38
+ cr: "Cree",
39
+ cs: "Czech",
40
+ cu: "Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic",
41
+ cv: "Chuvash",
42
+ cy: "Welsh",
43
+ da: "Danish",
44
+ de: "German",
45
+ dv: "Divehi; Dhivehi; Maldivian",
46
+ dz: "Dzongkha",
47
+ ee: "Ewe",
48
+ el: "Greek, Modern (1453-)",
49
+ en: "English",
50
+ eo: "Esperanto",
51
+ es: "Spanish; Castilian",
52
+ et: "Estonian",
53
+ eu: "Basque",
54
+ fa: "Persian",
55
+ ff: "Fulah",
56
+ fi: "Finnish",
57
+ fj: "Fijian",
58
+ fo: "Faroese",
59
+ fr: "French",
60
+ fy: "Western Frisian",
61
+ ga: "Irish",
62
+ gd: "Gaelic; Scottish Gaelic",
63
+ gl: "Galician",
64
+ gn: "Guarani",
65
+ gu: "Gujarati",
66
+ gv: "Manx",
67
+ ha: "Hausa",
68
+ he: "Hebrew",
69
+ hi: "Hindi",
70
+ ho: "Hiri Motu",
71
+ hr: "Croatian",
72
+ ht: "Haitian; Haitian Creole",
73
+ hu: "Hungarian",
74
+ hy: "Armenian",
75
+ hz: "Herero",
76
+ ia: "Interlingua (International Auxiliary Language Association)",
77
+ id: "Indonesian",
78
+ ie: "Interlingue; Occidental",
79
+ ig: "Igbo",
80
+ ii: "Sichuan Yi; Nuosu",
81
+ ik: "Inupiaq",
82
+ io: "Ido",
83
+ is: "Icelandic",
84
+ it: "Italian",
85
+ iu: "Inuktitut",
86
+ ja: "Japanese",
87
+ jv: "Javanese",
88
+ ka: "Georgian",
89
+ kg: "Kongo",
90
+ ki: "Kikuyu; Gikuyu",
91
+ kj: "Kuanyama; Kwanyama",
92
+ kk: "Kazakh",
93
+ kl: "Kalaallisut; Greenlandic",
94
+ km: "Central Khmer",
95
+ kn: "Kannada",
96
+ ko: "Korean",
97
+ kr: "Kanuri",
98
+ ks: "Kashmiri",
99
+ ku: "Kurdish",
100
+ kv: "Komi",
101
+ kw: "Cornish",
102
+ ky: "Kirghiz; Kyrgyz",
103
+ la: "Latin",
104
+ lb: "Luxembourgish; Letzeburgesch",
105
+ lg: "Ganda",
106
+ li: "Limburgan; Limburger; Limburgish",
107
+ ln: "Lingala",
108
+ lo: "Lao",
109
+ lt: "Lithuanian",
110
+ lu: "Luba-Katanga",
111
+ lv: "Latvian",
112
+ mg: "Malagasy",
113
+ mh: "Marshallese",
114
+ mi: "Maori",
115
+ mk: "Macedonian",
116
+ ml: "Malayalam",
117
+ mn: "Mongolian",
118
+ mr: "Marathi",
119
+ ms: "Malay",
120
+ mt: "Maltese",
121
+ my: "Burmese",
122
+ na: "Nauru",
123
+ nb: "Bokmål, Norwegian; Norwegian Bokmål",
124
+ nd: "Ndebele, North; North Ndebele",
125
+ ne: "Nepali",
126
+ ng: "Ndonga",
127
+ nl: "Dutch; Flemish",
128
+ nn: "Norwegian Nynorsk; Nynorsk, Norwegian",
129
+ no: "Norwegian",
130
+ nr: "Ndebele, South; South Ndebele",
131
+ nv: "Navajo; Navaho",
132
+ ny: "Chichewa; Chewa; Nyanja",
133
+ oc: "Occitan (post 1500)",
134
+ oj: "Ojibwa",
135
+ om: "Oromo",
136
+ or: "Oriya",
137
+ os: "Ossetian; Ossetic",
138
+ pa: "Panjabi; Punjabi",
139
+ pi: "Pali",
140
+ pl: "Polish",
141
+ ps: "Pushto; Pashto",
142
+ pt: "Portuguese",
143
+ qu: "Quechua",
144
+ rm: "Romansh",
145
+ rn: "Rundi",
146
+ ro: "Romanian; Moldavian; Moldovan",
147
+ ru: "Russian",
148
+ rw: "Kinyarwanda",
149
+ sa: "Sanskrit",
150
+ sc: "Sardinian",
151
+ sd: "Sindhi",
152
+ se: "Northern Sami",
153
+ sg: "Sango",
154
+ si: "Sinhala; Sinhalese",
155
+ sk: "Slovak",
156
+ sl: "Slovenian",
157
+ sm: "Samoan",
158
+ sn: "Shona",
159
+ so: "Somali",
160
+ sq: "Albanian",
161
+ sr: "Serbian",
162
+ ss: "Swati",
163
+ st: "Sotho, Southern",
164
+ su: "Sundanese",
165
+ sv: "Swedish",
166
+ sw: "Swahili",
167
+ ta: "Tamil",
168
+ te: "Telugu",
169
+ tg: "Tajik",
170
+ th: "Thai",
171
+ ti: "Tigrinya",
172
+ tk: "Turkmen",
173
+ tl: "Tagalog",
174
+ tn: "Tswana",
175
+ to: "Tonga (Tonga Islands)",
176
+ tr: "Turkish",
177
+ ts: "Tsonga",
178
+ tt: "Tatar",
179
+ tw: "Twi",
180
+ ty: "Tahitian",
181
+ ug: "Uighur; Uyghur",
182
+ uk: "Ukrainian",
183
+ ur: "Urdu",
184
+ uz: "Uzbek",
185
+ ve: "Venda",
186
+ vi: "Vietnamese",
187
+ vo: "Volapük",
188
+ wa: "Walloon",
189
+ wo: "Wolof",
190
+ xh: "Xhosa",
191
+ yi: "Yiddish",
192
+ yo: "Yoruba",
193
+ za: "Zhuang; Chuang",
194
+ zh: "Chinese",
195
+ zu: "Zulu"
196
+ };
197
+ var ALL_LANG_CODES = (0, _toConsumableArray2["default"])(new Set(Object.keys(LANG_CODE_NAME_MAPPING)));
198
+ var _default = ALL_LANG_CODES;
199
+ exports["default"] = _default;
200
+ module.exports = exports.default;
@@ -84,6 +84,7 @@ var Subscriber = /*#__PURE__*/function () {
84
84
  this.__info = [];
85
85
  this._append_count = 0;
86
86
  this._remove_count = 0;
87
+ this._set_count = 0;
87
88
  this._unset_count = 0;
88
89
  this._events = [];
89
90
  this._helper = new _subscriber_helper["default"](distinct_id, config.workspace_key);
@@ -139,7 +140,7 @@ var Subscriber = /*#__PURE__*/function () {
139
140
  _iterator.f();
140
141
  }
141
142
 
142
- if (all_events.length === 0 || this._append_count > 0) {
143
+ if (all_events.length === 0 || this._append_count > 0 || this._set_count > 0) {
143
144
  var user_identify_event = {
144
145
  $insert_id: (0, _utils.uuid)(),
145
146
  $time: (0, _utils.epoch_milliseconds)(),
@@ -300,6 +301,7 @@ var Subscriber = /*#__PURE__*/function () {
300
301
  if (!(0, _utils.is_empty)(resp["event"])) {
301
302
  this._events.push(resp["event"]);
302
303
 
304
+ this._set_count += resp["set"];
303
305
  this._append_count += resp["append"];
304
306
  this._remove_count += resp["remove"];
305
307
  this._unset_count += resp["unset"];
@@ -397,6 +399,15 @@ var Subscriber = /*#__PURE__*/function () {
397
399
  this._collect_event();
398
400
  }
399
401
  }
402
+ }, {
403
+ key: "set_preferred_language",
404
+ value: function set_preferred_language(lang_code) {
405
+ var caller = "set_preferred_language";
406
+
407
+ this._helper._set_preferred_language(lang_code, caller);
408
+
409
+ this._collect_event();
410
+ }
400
411
  }, {
401
412
  key: "add_email",
402
413
  value: function add_email(email) {
@@ -15,6 +15,8 @@ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/creat
15
15
 
16
16
  var _utils = require("./utils");
17
17
 
18
+ var _language_codes = _interopRequireDefault(require("./language_codes"));
19
+
18
20
  // ---------- Identity keys
19
21
  var IDENT_KEY_EMAIL = "$email";
20
22
  var IDENT_KEY_SMS = "$sms";
@@ -25,7 +27,8 @@ var IDENT_KEY_WEBPUSH = "$webpush";
25
27
  var IDENT_KEY_SLACK = "$slack";
26
28
  var IDENT_KEYS_ALL = [IDENT_KEY_EMAIL, IDENT_KEY_SMS, IDENT_KEY_ANDROIDPUSH, IDENT_KEY_IOSPUSH, IDENT_KEY_WHATSAPP, IDENT_KEY_WEBPUSH, IDENT_KEY_SLACK];
27
29
  var KEY_PUSHVENDOR = "$pushvendor";
28
- var OTHER_RESERVED_KEYS = ["$messenger", "$inbox", KEY_PUSHVENDOR, "$device_id", "$insert_id", "$time", "$set", "$set_once", "$add", "$append", "$remove", "$unset", "$identify", "$anon_id", "$identified_id", "$notification_delivered", "$notification_dismiss", "$notification_clicked"];
30
+ var KEY_PREFERRED_LANGUAGE = "$preferred_language";
31
+ var OTHER_RESERVED_KEYS = ["$messenger", "$inbox", KEY_PUSHVENDOR, "$device_id", "$insert_id", "$time", "$set", "$set_once", "$add", "$append", "$remove", "$unset", "$identify", "$anon_id", "$identified_id", KEY_PREFERRED_LANGUAGE, "$notification_delivered", "$notification_dismiss", "$notification_clicked"];
29
32
  var SUPER_PROPERTY_KEYS = ["$app_version_string", "$app_build_number", "$brand", "$carrier", "$manufacturer", "$model", "$os", "$ss_sdk_version", "$insert_id", "$time"];
30
33
  var ALL_RESERVED_KEYS = [].concat(SUPER_PROPERTY_KEYS, OTHER_RESERVED_KEYS, IDENT_KEYS_ALL);
31
34
  var EMAIL_REGEX = /\S+@\S+\.\S+/;
@@ -36,6 +39,8 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
36
39
  (0, _classCallCheck2["default"])(this, _SubscriberInternalHelper);
37
40
  this.distinct_id = distinct_id;
38
41
  this.workspace_key = workspace_key;
42
+ this.__dict_set = {};
43
+ this.__set_count = 0;
39
44
  this.__dict_append = {};
40
45
  this.__append_count = 0;
41
46
  this.__dict_remove = {};
@@ -49,6 +54,8 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
49
54
  (0, _createClass2["default"])(_SubscriberInternalHelper, [{
50
55
  key: "reset",
51
56
  value: function reset() {
57
+ this.__dict_set = {};
58
+ this.__set_count = 0;
52
59
  this.__dict_append = {};
53
60
  this.__append_count = 0;
54
61
  this.__dict_remove = {};
@@ -67,6 +74,7 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
67
74
  errors: this.__errors,
68
75
  info: this.__info,
69
76
  event: evt,
77
+ set: this.__set_count,
70
78
  append: this.__append_count,
71
79
  remove: this.__remove_count,
72
80
  unset: this.__unset_count
@@ -77,7 +85,7 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
77
85
  }, {
78
86
  key: "__form_event",
79
87
  value: function __form_event() {
80
- if (!(0, _utils.is_empty)(this.__dict_append) || !(0, _utils.is_empty)(this.__dict_remove) || !(0, _utils.is_empty)(this.__list_unset)) {
88
+ if (!(0, _utils.is_empty)(this.__dict_set) || !(0, _utils.is_empty)(this.__dict_append) || !(0, _utils.is_empty)(this.__dict_remove) || !(0, _utils.is_empty)(this.__list_unset)) {
81
89
  var event = {
82
90
  $insert_id: (0, _utils.uuid)(),
83
91
  $time: (0, _utils.epoch_milliseconds)(),
@@ -85,6 +93,11 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
85
93
  distinct_id: this.distinct_id
86
94
  };
87
95
 
96
+ if (!(0, _utils.is_empty)(this.__dict_set)) {
97
+ event["$set"] = this.__dict_set;
98
+ this.__set_count += 1;
99
+ }
100
+
88
101
  if (!(0, _utils.is_empty)(this.__dict_append)) {
89
102
  event["$append"] = this.__dict_append;
90
103
  this.__append_count += 1;
@@ -208,6 +221,17 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
208
221
 
209
222
  this.__list_unset.push(validated_key);
210
223
  }
224
+ }, {
225
+ key: "_set_preferred_language",
226
+ value: function _set_preferred_language(lang_code, caller) {
227
+ if (!_language_codes["default"].includes(lang_code)) {
228
+ this.__info.push("[".concat(caller, "] invalid value ").concat(lang_code));
229
+
230
+ return;
231
+ }
232
+
233
+ this.__dict_set[KEY_PREFERRED_LANGUAGE] = lang_code;
234
+ }
211
235
  }, {
212
236
  key: "__add_identity",
213
237
  value: function __add_identity(key, value, args, caller) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -0,0 +1,188 @@
1
+ const LANG_CODE_NAME_MAPPING = {
2
+ aa: "Afar",
3
+ ab: "Abkhazian",
4
+ ae: "Avestan",
5
+ af: "Afrikaans",
6
+ ak: "Akan",
7
+ am: "Amharic",
8
+ an: "Aragonese",
9
+ ar: "Arabic",
10
+ as: "Assamese",
11
+ av: "Avaric",
12
+ ay: "Aymara",
13
+ az: "Azerbaijani",
14
+ ba: "Bashkir",
15
+ be: "Belarusian",
16
+ bg: "Bulgarian",
17
+ bi: "Bislama",
18
+ bm: "Bambara",
19
+ bn: "Bengali",
20
+ bo: "Tibetan",
21
+ br: "Breton",
22
+ bs: "Bosnian",
23
+ ca: "Catalan; Valencian",
24
+ ce: "Chechen",
25
+ ch: "Chamorro",
26
+ co: "Corsican",
27
+ cr: "Cree",
28
+ cs: "Czech",
29
+ cu: "Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic",
30
+ cv: "Chuvash",
31
+ cy: "Welsh",
32
+ da: "Danish",
33
+ de: "German",
34
+ dv: "Divehi; Dhivehi; Maldivian",
35
+ dz: "Dzongkha",
36
+ ee: "Ewe",
37
+ el: "Greek, Modern (1453-)",
38
+ en: "English",
39
+ eo: "Esperanto",
40
+ es: "Spanish; Castilian",
41
+ et: "Estonian",
42
+ eu: "Basque",
43
+ fa: "Persian",
44
+ ff: "Fulah",
45
+ fi: "Finnish",
46
+ fj: "Fijian",
47
+ fo: "Faroese",
48
+ fr: "French",
49
+ fy: "Western Frisian",
50
+ ga: "Irish",
51
+ gd: "Gaelic; Scottish Gaelic",
52
+ gl: "Galician",
53
+ gn: "Guarani",
54
+ gu: "Gujarati",
55
+ gv: "Manx",
56
+ ha: "Hausa",
57
+ he: "Hebrew",
58
+ hi: "Hindi",
59
+ ho: "Hiri Motu",
60
+ hr: "Croatian",
61
+ ht: "Haitian; Haitian Creole",
62
+ hu: "Hungarian",
63
+ hy: "Armenian",
64
+ hz: "Herero",
65
+ ia: "Interlingua (International Auxiliary Language Association)",
66
+ id: "Indonesian",
67
+ ie: "Interlingue; Occidental",
68
+ ig: "Igbo",
69
+ ii: "Sichuan Yi; Nuosu",
70
+ ik: "Inupiaq",
71
+ io: "Ido",
72
+ is: "Icelandic",
73
+ it: "Italian",
74
+ iu: "Inuktitut",
75
+ ja: "Japanese",
76
+ jv: "Javanese",
77
+ ka: "Georgian",
78
+ kg: "Kongo",
79
+ ki: "Kikuyu; Gikuyu",
80
+ kj: "Kuanyama; Kwanyama",
81
+ kk: "Kazakh",
82
+ kl: "Kalaallisut; Greenlandic",
83
+ km: "Central Khmer",
84
+ kn: "Kannada",
85
+ ko: "Korean",
86
+ kr: "Kanuri",
87
+ ks: "Kashmiri",
88
+ ku: "Kurdish",
89
+ kv: "Komi",
90
+ kw: "Cornish",
91
+ ky: "Kirghiz; Kyrgyz",
92
+ la: "Latin",
93
+ lb: "Luxembourgish; Letzeburgesch",
94
+ lg: "Ganda",
95
+ li: "Limburgan; Limburger; Limburgish",
96
+ ln: "Lingala",
97
+ lo: "Lao",
98
+ lt: "Lithuanian",
99
+ lu: "Luba-Katanga",
100
+ lv: "Latvian",
101
+ mg: "Malagasy",
102
+ mh: "Marshallese",
103
+ mi: "Maori",
104
+ mk: "Macedonian",
105
+ ml: "Malayalam",
106
+ mn: "Mongolian",
107
+ mr: "Marathi",
108
+ ms: "Malay",
109
+ mt: "Maltese",
110
+ my: "Burmese",
111
+ na: "Nauru",
112
+ nb: "Bokmål, Norwegian; Norwegian Bokmål",
113
+ nd: "Ndebele, North; North Ndebele",
114
+ ne: "Nepali",
115
+ ng: "Ndonga",
116
+ nl: "Dutch; Flemish",
117
+ nn: "Norwegian Nynorsk; Nynorsk, Norwegian",
118
+ no: "Norwegian",
119
+ nr: "Ndebele, South; South Ndebele",
120
+ nv: "Navajo; Navaho",
121
+ ny: "Chichewa; Chewa; Nyanja",
122
+ oc: "Occitan (post 1500)",
123
+ oj: "Ojibwa",
124
+ om: "Oromo",
125
+ or: "Oriya",
126
+ os: "Ossetian; Ossetic",
127
+ pa: "Panjabi; Punjabi",
128
+ pi: "Pali",
129
+ pl: "Polish",
130
+ ps: "Pushto; Pashto",
131
+ pt: "Portuguese",
132
+ qu: "Quechua",
133
+ rm: "Romansh",
134
+ rn: "Rundi",
135
+ ro: "Romanian; Moldavian; Moldovan",
136
+ ru: "Russian",
137
+ rw: "Kinyarwanda",
138
+ sa: "Sanskrit",
139
+ sc: "Sardinian",
140
+ sd: "Sindhi",
141
+ se: "Northern Sami",
142
+ sg: "Sango",
143
+ si: "Sinhala; Sinhalese",
144
+ sk: "Slovak",
145
+ sl: "Slovenian",
146
+ sm: "Samoan",
147
+ sn: "Shona",
148
+ so: "Somali",
149
+ sq: "Albanian",
150
+ sr: "Serbian",
151
+ ss: "Swati",
152
+ st: "Sotho, Southern",
153
+ su: "Sundanese",
154
+ sv: "Swedish",
155
+ sw: "Swahili",
156
+ ta: "Tamil",
157
+ te: "Telugu",
158
+ tg: "Tajik",
159
+ th: "Thai",
160
+ ti: "Tigrinya",
161
+ tk: "Turkmen",
162
+ tl: "Tagalog",
163
+ tn: "Tswana",
164
+ to: "Tonga (Tonga Islands)",
165
+ tr: "Turkish",
166
+ ts: "Tsonga",
167
+ tt: "Tatar",
168
+ tw: "Twi",
169
+ ty: "Tahitian",
170
+ ug: "Uighur; Uyghur",
171
+ uk: "Ukrainian",
172
+ ur: "Urdu",
173
+ uz: "Uzbek",
174
+ ve: "Venda",
175
+ vi: "Vietnamese",
176
+ vo: "Volapük",
177
+ wa: "Walloon",
178
+ wo: "Wolof",
179
+ xh: "Xhosa",
180
+ yi: "Yiddish",
181
+ yo: "Yoruba",
182
+ za: "Zhuang; Chuang",
183
+ zh: "Chinese",
184
+ zu: "Zulu",
185
+ };
186
+
187
+ const ALL_LANG_CODES = [...new Set(Object.keys(LANG_CODE_NAME_MAPPING))];
188
+ export default ALL_LANG_CODES;
package/src/subscriber.js CHANGED
@@ -50,6 +50,7 @@ export class Subscriber {
50
50
  this.__info = [];
51
51
  this._append_count = 0;
52
52
  this._remove_count = 0;
53
+ this._set_count = 0;
53
54
  this._unset_count = 0;
54
55
  this._events = [];
55
56
  this._helper = new _SubscriberInternalHelper(
@@ -90,7 +91,11 @@ export class Subscriber {
90
91
  e["properties"] = this.__supr_props;
91
92
  }
92
93
 
93
- if (all_events.length === 0 || this._append_count > 0) {
94
+ if (
95
+ all_events.length === 0 ||
96
+ this._append_count > 0 ||
97
+ this._set_count > 0
98
+ ) {
94
99
  const user_identify_event = {
95
100
  $insert_id: uuid(),
96
101
  $time: epoch_milliseconds(),
@@ -196,6 +201,7 @@ export class Subscriber {
196
201
  }
197
202
  if (!is_empty(resp["event"])) {
198
203
  this._events.push(resp["event"]);
204
+ this._set_count += resp["set"];
199
205
  this._append_count += resp["append"];
200
206
  this._remove_count += resp["remove"];
201
207
  this._unset_count += resp["unset"];
@@ -267,6 +273,12 @@ export class Subscriber {
267
273
  }
268
274
  }
269
275
 
276
+ set_preferred_language(lang_code) {
277
+ const caller = "set_preferred_language";
278
+ this._helper._set_preferred_language(lang_code, caller);
279
+ this._collect_event();
280
+ }
281
+
270
282
  add_email(email) {
271
283
  const caller = "add_email";
272
284
  this._helper._add_email(email, caller);
@@ -6,6 +6,7 @@ import {
6
6
  is_empty,
7
7
  is_string,
8
8
  } from "./utils";
9
+ import ALL_LANG_CODES from "./language_codes";
9
10
 
10
11
  // ---------- Identity keys
11
12
  const IDENT_KEY_EMAIL = "$email";
@@ -27,6 +28,7 @@ const IDENT_KEYS_ALL = [
27
28
  ];
28
29
 
29
30
  const KEY_PUSHVENDOR = "$pushvendor";
31
+ const KEY_PREFERRED_LANGUAGE = "$preferred_language";
30
32
 
31
33
  const OTHER_RESERVED_KEYS = [
32
34
  "$messenger",
@@ -44,6 +46,7 @@ const OTHER_RESERVED_KEYS = [
44
46
  "$identify",
45
47
  "$anon_id",
46
48
  "$identified_id",
49
+ KEY_PREFERRED_LANGUAGE,
47
50
  "$notification_delivered",
48
51
  "$notification_dismiss",
49
52
  "$notification_clicked",
@@ -76,6 +79,9 @@ export default class _SubscriberInternalHelper {
76
79
  this.distinct_id = distinct_id;
77
80
  this.workspace_key = workspace_key;
78
81
 
82
+ this.__dict_set = {};
83
+ this.__set_count = 0;
84
+
79
85
  this.__dict_append = {};
80
86
  this.__append_count = 0;
81
87
 
@@ -90,6 +96,9 @@ export default class _SubscriberInternalHelper {
90
96
  }
91
97
 
92
98
  reset() {
99
+ this.__dict_set = {};
100
+ this.__set_count = 0;
101
+
93
102
  this.__dict_append = {};
94
103
  this.__append_count = 0;
95
104
 
@@ -109,6 +118,7 @@ export default class _SubscriberInternalHelper {
109
118
  errors: this.__errors,
110
119
  info: this.__info,
111
120
  event: evt,
121
+ set: this.__set_count,
112
122
  append: this.__append_count,
113
123
  remove: this.__remove_count,
114
124
  unset: this.__unset_count,
@@ -119,6 +129,7 @@ export default class _SubscriberInternalHelper {
119
129
 
120
130
  __form_event() {
121
131
  if (
132
+ !is_empty(this.__dict_set) ||
122
133
  !is_empty(this.__dict_append) ||
123
134
  !is_empty(this.__dict_remove) ||
124
135
  !is_empty(this.__list_unset)
@@ -129,6 +140,10 @@ export default class _SubscriberInternalHelper {
129
140
  env: this.workspace_key,
130
141
  distinct_id: this.distinct_id,
131
142
  };
143
+ if (!is_empty(this.__dict_set)) {
144
+ event["$set"] = this.__dict_set;
145
+ this.__set_count += 1;
146
+ }
132
147
  if (!is_empty(this.__dict_append)) {
133
148
  event["$append"] = this.__dict_append;
134
149
  this.__append_count += 1;
@@ -216,6 +231,14 @@ export default class _SubscriberInternalHelper {
216
231
  this.__list_unset.push(validated_key);
217
232
  }
218
233
 
234
+ _set_preferred_language(lang_code, caller) {
235
+ if (!ALL_LANG_CODES.includes(lang_code)) {
236
+ this.__info.push(`[${caller}] invalid value ${lang_code}`);
237
+ return;
238
+ }
239
+ this.__dict_set[KEY_PREFERRED_LANGUAGE] = lang_code;
240
+ }
241
+
219
242
  __add_identity(key, value, args, caller) {
220
243
  switch (key) {
221
244
  case IDENT_KEY_EMAIL: