hina-cloud-js-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,388 @@
1
+ import { _, Log, SearchKeyword } from "./utils";
2
+
3
+ function checkIsNewUser() {
4
+ if (_.cookie.isSupport()) {
5
+ if (_.cookie.get("hinasdk_isNewUser") != null) {
6
+ return true;
7
+ }
8
+ return false;
9
+ } else {
10
+ if (_.memory.get("hinasdk_isNewUser") != null) {
11
+ return true;
12
+ }
13
+ return false;
14
+ }
15
+ }
16
+
17
+ /*
18
+ *通过cookie或者memory存储加密后的data, 需要执行一次load方法
19
+ *
20
+ */
21
+ let HinaDataStore = {
22
+ name: "hinasdk_crossdata",
23
+ state: {
24
+ deviceId: null,
25
+ accountId: null,
26
+ firstId: null,
27
+ //首次访问时间,用户属性用于触发profile_set_once接口
28
+ firstVisitTime: _.now(),
29
+ props: {},
30
+ },
31
+ //首次触发事件
32
+ isFirstTime: false,
33
+ //首日触发事件
34
+ isFirstDay: checkIsNewUser(),
35
+ //第一次访问
36
+ isFirstVisit: true,
37
+ load() {
38
+ let oldCookie = null;
39
+ if (_.cookie.isSupport()) {
40
+ this.storage = _.cookie;
41
+ } else {
42
+ Log.log(
43
+ "Cookie storage is not supported, SDK internal cache has been enabled"
44
+ );
45
+ this.storage = _.memory;
46
+ }
47
+
48
+ if (!oldCookie) {
49
+ oldCookie = this.storage.get(this.name);
50
+ }
51
+
52
+ if (oldCookie && _.check.isJSONString(oldCookie)) {
53
+ this.state = _.extend({}, JSON.parse(oldCookie));
54
+ }
55
+
56
+ //如果不存在hinasdk_crossdata的存储数据,则说明是第一次访问sdk
57
+ if (oldCookie) {
58
+ this.save();
59
+ this.isFirstVisit = false;
60
+ } else {
61
+ this.isFirstVisit = true;
62
+ }
63
+
64
+ // 第一次访问sdk必定是新用户
65
+ if (this.isFirstVisit) {
66
+ let date = new Date();
67
+ let dateObj = {
68
+ h: 23 - date.getHours(),
69
+ m: 59 - date.getMinutes(),
70
+ s: 59 - date.getSeconds(),
71
+ };
72
+ this.storage.set(
73
+ "hinasdk_isNewUser",
74
+ true,
75
+ dateObj.h * 3600 + dateObj.m * 60 + dateObj.s + "s"
76
+ );
77
+ this.isFirstDay = true;
78
+ this.isFirstTime = true;
79
+ } else {
80
+ //由于首次访问sdk会触发pageview事件,第二次触发pageview事件开始只返回false
81
+ this.checkIsFirstTime = function (data) {
82
+ if (data.type === "track" && data.event === "H_pageview") {
83
+ data.properties.H_is_first_time = false;
84
+ }
85
+ };
86
+ }
87
+
88
+ if (!this.getAccountId()) {
89
+ let uuid = _.UUID();
90
+ this.setDeviceId(uuid);
91
+ this.setAccountId(uuid);
92
+ }
93
+ },
94
+
95
+ checkIsFirstTime(data) {
96
+ if (data.type === "track" && data.event === "H_pageview") {
97
+ if (checkIsNewUser() && this.isFirstTime) {
98
+ data.properties.H_is_first_time = true;
99
+ this.isFirstTime = false;
100
+ } else {
101
+ data.properties.H_is_first_time = false;
102
+ }
103
+ }
104
+ },
105
+
106
+ checkIsFirstSign(data) {
107
+ if (data.type === "track") {
108
+ if (checkIsNewUser() && this.isFirstDay) {
109
+ data.properties.H_is_first_day = true;
110
+ } else {
111
+ this.isFirstDay = false;
112
+ data.properties.H_is_first_day = false;
113
+ }
114
+ }
115
+ },
116
+
117
+ setDeviceId(uuid) {
118
+ if (this.state.deviceId) {
119
+ Log.log(
120
+ "Current deviceId is " + this.getDeviceId() + ", it has been set"
121
+ );
122
+ return;
123
+ }
124
+ this.set("deviceId", uuid);
125
+ },
126
+
127
+ setAccountId(accountId) {
128
+ this.set("accountId", accountId);
129
+ },
130
+
131
+ getDeviceId() {
132
+ return this.state.deviceId;
133
+ },
134
+
135
+ getAccountId() {
136
+ return this.state.__accountId || this.state.accountId;
137
+ },
138
+
139
+ getFirstId() {
140
+ return this.state.__firstId || this.state.firstId;
141
+ },
142
+
143
+ getAnonymousId() {
144
+ let accountId = this.getAccountId();
145
+ let firstId = this.getFirstId();
146
+ let anonymousId = null;
147
+ if (accountId && firstId) {
148
+ anonymousId = firstId;
149
+ } else {
150
+ anonymousId = accountId;
151
+ }
152
+ return anonymousId;
153
+ },
154
+
155
+ change(name, value) {
156
+ this.state["__" + name] = value;
157
+ },
158
+
159
+ set(name = "", value) {
160
+ this.state = this.state || {};
161
+ if (["accountId", "firstId"].indexOf(name) > -1) {
162
+ delete this.state["__" + name];
163
+ }
164
+ this.state[name] = value;
165
+ this.save();
166
+ },
167
+
168
+ save() {
169
+ this.storage.set(this.name, JSON.stringify(this.state), null, true);
170
+ },
171
+
172
+ clear() {
173
+ this.state = {};
174
+ this.save();
175
+ },
176
+ };
177
+
178
+ let pageInfo = {
179
+ pageProp: {},
180
+ currentProps: {},
181
+ register: function (obj) {
182
+ _.extend(pageInfo.currentProps, obj);
183
+ },
184
+ getPresetProperties: function () {
185
+ let viewportHeightValue =
186
+ window.innerHeight ||
187
+ document.documentElement.clientHeight ||
188
+ (document.body && document.body.clientHeight) ||
189
+ 0;
190
+ let viewportWidthValue =
191
+ window.innerWidth ||
192
+ document.documentElement.clientWidth ||
193
+ (document.body && document.body.clientWidth) ||
194
+ 0;
195
+
196
+ let propertiesObj = {
197
+ H_timezone_offset: new Date().getTimezoneOffset(),
198
+ H_viewport_height: viewportHeightValue,
199
+ H_viewport_width: viewportWidthValue,
200
+ };
201
+ _.extend(propertiesObj, _.info.properties());
202
+ return propertiesObj;
203
+ },
204
+ getPageProperties: function () {
205
+ _.extend(this.pageProp, _.info.pageProperties());
206
+ return this.pageProp;
207
+ },
208
+ getUmtsParams: function (prefix = "", prefixAdd = "") {
209
+ let utms = _.getUtm();
210
+ let allUtms = {},
211
+ otherUtms = {};
212
+ _.each(utms, function (d, i, utms) {
213
+ if (_.utmTypes.includes(i)) {
214
+ allUtms[prefix + i] = utms[i];
215
+ } else {
216
+ otherUtms[prefixAdd + i] = utms[i];
217
+ }
218
+ });
219
+ return {
220
+ allUtms: allUtms,
221
+ otherUtms: otherUtms,
222
+ };
223
+ },
224
+ };
225
+
226
+ function initLatestProps(para) {
227
+ let latestObj = {};
228
+
229
+ _.each(para.presetProperties, function (value, key) {
230
+ if (key.indexOf("latest_") === -1) {
231
+ return false;
232
+ }
233
+ key = key.slice(7);
234
+ if (value) {
235
+ let url_domain = _.getCurrentDomain(window.location.href);
236
+ if (key !== "utm" && url_domain === "url解析失败") {
237
+ latestObj["H_latest_" + key] = "url的domain解析失败";
238
+ } else if (_.isReferralTraffic(document.referrer)) {
239
+ switch (key) {
240
+ case "traffic_source_type":
241
+ latestObj["H_latest_traffic_source_type"] =
242
+ SearchKeyword.getSourceFromReferrer();
243
+ break;
244
+
245
+ case "referrer":
246
+ latestObj["H_latest_referrer"] = pageInfo.pageProp.referrer;
247
+ break;
248
+
249
+ case "search_keyword":
250
+ if (SearchKeyword.getKeywordFromReferrer()) {
251
+ latestObj["H_latest_search_keyword"] =
252
+ SearchKeyword.getKeywordFromReferrer();
253
+ }
254
+ break;
255
+
256
+ default:
257
+ break;
258
+ }
259
+ }
260
+ }
261
+ });
262
+
263
+ if (para.presetProperties.latest_utm) {
264
+ let { allUtms, otherUtms } = pageInfo.getUmtsParams(
265
+ "H_latest_",
266
+ "_latest_"
267
+ );
268
+ if (!_.check.isEmptyObject(allUtms)) {
269
+ _.extend(latestObj, allUtms);
270
+ }
271
+ if (!_.check.isEmptyObject(otherUtms)) {
272
+ _.extend(latestObj, otherUtms);
273
+ }
274
+ }
275
+ pageInfo.register(latestObj);
276
+ }
277
+
278
+ function getPresetProperties() {
279
+ let { allUtms, otherUtms } = pageInfo.getUmtsParams("H_", "");
280
+
281
+ let result = {
282
+ H_is_first_day: HinaDataStore["isFirstDay"],
283
+ H_is_first_time: HinaDataStore["isFirstTime"],
284
+ device_id: HinaDataStore.getDeviceId(),
285
+ anonymous_id: HinaDataStore.getAnonymousId(),
286
+ account_id: HinaDataStore.getAccountId(),
287
+ properties: {
288
+ ...HinaDataStore.state["props"],
289
+ },
290
+ };
291
+
292
+ _.extend(
293
+ result.properties,
294
+ allUtms,
295
+ otherUtms,
296
+ pageInfo.getPresetProperties(),
297
+ pageInfo.getPageProperties()
298
+ );
299
+
300
+ return result;
301
+ }
302
+
303
+ function sendFirstProfile(setOnceProfileFn, fullReferrer, ctx) {
304
+ if (HinaDataStore.isFirstVisit) {
305
+ let referrer = _.getReferrer(null, fullReferrer);
306
+ setOnceProfileFn.call(
307
+ ctx,
308
+ _.extend({
309
+ H_first_visit_time: _.now(),
310
+ H_first_referrer: referrer,
311
+ H_first_host: referrer ? _.getHostname(referrer, "取值异常") : "",
312
+ H_first_browser_language: _.check.isString(navigator.languages[1])
313
+ ? navigator.languages[1].toLowerCase()
314
+ : "取值异常",
315
+ H_first_traffic_source_type: SearchKeyword.getSourceFromReferrer(),
316
+ H_first_search_keyword: SearchKeyword.getKeywordFromReferrer(),
317
+ })
318
+ );
319
+ HinaDataStore.isFirstVisit = false;
320
+ }
321
+ }
322
+
323
+ function addProps(p, hn) {
324
+ let config = hn.config || {};
325
+ let { allUtms, otherUtms } = pageInfo.getUmtsParams("H_", "");
326
+
327
+ let data = {
328
+ anonymous_id: HinaDataStore.getAnonymousId(),
329
+ properties: {
330
+ H_lib: "js",
331
+ H_lib_version: _.LIB_VERSION,
332
+ H_lib_method: "code",
333
+ ...HinaDataStore.state["props"],
334
+ },
335
+ type: p.type,
336
+ event: p.event,
337
+ time: _.now(),
338
+ _track_id: Number(
339
+ String(_.getRandom()).slice(2, 5) +
340
+ String(_.getRandom()).slice(2, 4) +
341
+ String(_.now()).slice(-4)
342
+ ),
343
+ };
344
+
345
+ if (HinaDataStore.getAccountId()) {
346
+ data.account_id = HinaDataStore.getAccountId();
347
+ }
348
+
349
+ // if(config["name"]){
350
+ // data.properties.name = config["name"];
351
+ // }
352
+
353
+ if (config["isTrackDeviceId"]) {
354
+ data.device_id = HinaDataStore.getDeviceId();
355
+ }
356
+
357
+ // track、track_signup
358
+ // user_set、user_setOnce、user_add、user_unset、user_delete
359
+ if (!p.type || p.type.slice(0, 4) !== "user") {
360
+ data.properties = _.extend(
361
+ data.properties,
362
+ allUtms,
363
+ otherUtms,
364
+ pageInfo.currentProps,
365
+ pageInfo.getPageProperties(),
366
+ pageInfo.getPresetProperties(),
367
+ p.properties
368
+ );
369
+ } else {
370
+ data.properties = _.extend(data.properties, p.properties);
371
+ }
372
+
373
+ _.parseSuperProperties(data);
374
+
375
+ HinaDataStore.checkIsFirstSign(data);
376
+ HinaDataStore.checkIsFirstTime(data);
377
+
378
+ return data;
379
+ }
380
+
381
+ export {
382
+ HinaDataStore,
383
+ pageInfo,
384
+ initLatestProps,
385
+ getPresetProperties,
386
+ sendFirstProfile,
387
+ addProps,
388
+ };