|
| 1 | +sensorsDataAnalytic201505.modules['SessionEvent'] = (function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /** |
| 5 | + * 添加自定义属性 |
| 6 | + * |
| 7 | + * @param { JSON } data 数据日志 |
| 8 | + * @param { Object } instance 当前 use 插件的实例 |
| 9 | + * @return { JSON } 添加完毕后的数据日志 |
| 10 | + */ |
| 11 | + function addProperties(data, instance) { |
| 12 | + if (data.type !== 'track') return data; |
| 13 | + var sd = instance.sd; |
| 14 | + var _ = sd._; |
| 15 | + var check = sd.saEvent.check; |
| 16 | + |
| 17 | + // 克隆数据,阻止 hookRegister 中无法对原有 data 的更改 |
| 18 | + var copyData = _.extend2Lev({ properties: {} }, data); |
| 19 | + var currentProps = instance.currentProps; |
| 20 | + var properties = copyData.properties; |
| 21 | + var event = copyData.event; |
| 22 | + var props = {}; |
| 23 | + |
| 24 | + _.each(currentProps, function (prop) { |
| 25 | + if (_.isObject(prop)) { |
| 26 | + if (prop.events.indexOf(event) > -1) { |
| 27 | + // 校验属性及属性值是否合法 |
| 28 | + if (check({ properties: prop.properties })) { |
| 29 | + props = _.extend(props, prop.properties); |
| 30 | + } |
| 31 | + } |
| 32 | + } else if (_.isFunction(prop)) { |
| 33 | + var callbackProp = prop({ |
| 34 | + event: event, |
| 35 | + properties: properties, |
| 36 | + data: copyData |
| 37 | + }); |
| 38 | + // 校验属性及属性值是否合法 |
| 39 | + if (_.isObject(callbackProp) && !_.isEmptyObject(callbackProp) && check({ properties: callbackProp })) { |
| 40 | + props = _.extend(props, callbackProp); |
| 41 | + } |
| 42 | + } |
| 43 | + }); |
| 44 | + data.properties = _.extend(properties, props); |
| 45 | + return data; |
| 46 | + } |
| 47 | + |
| 48 | + function DataStageImpl(registerInstance) { |
| 49 | + var _this = this; |
| 50 | + this.sd = registerInstance.sd; |
| 51 | + this.currentProps = registerInstance.customRegister; |
| 52 | + this.interceptor = { |
| 53 | + addCustomProps: { |
| 54 | + priority: 0, |
| 55 | + entry: function (data) { |
| 56 | + addProperties(data, _this); |
| 57 | + return data; |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | + /** |
| 63 | + * DataStageImpl 必须有 init 方法,可以为空方法 |
| 64 | + */ |
| 65 | + DataStageImpl.prototype.init = function () {}; |
| 66 | + |
| 67 | + function registerPropertiesFeature(registerInstance) { |
| 68 | + // assign dataStage for registerFeature to find the dataStage implementation |
| 69 | + this.dataStage = new DataStageImpl(registerInstance); |
| 70 | + } |
| 71 | + |
| 72 | + function RegisterProperties() { |
| 73 | + this.sd = null; |
| 74 | + this.log = (window.console && window.console.log) || function () {}; |
| 75 | + this.customRegister = []; |
| 76 | + } |
| 77 | + RegisterProperties.prototype.init = function (sd) { |
| 78 | + if (sd) { |
| 79 | + this.sd = sd; |
| 80 | + this._ = sd._; |
| 81 | + this.log = sd.log; |
| 82 | + sd.registerFeature(new registerPropertiesFeature(this)); |
| 83 | + } else { |
| 84 | + this.log('神策JS SDK未成功引入'); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + RegisterProperties.prototype.register = function (customProps) { |
| 89 | + if (!this.sd) { |
| 90 | + this.log('神策JS SDK未成功引入'); |
| 91 | + return; |
| 92 | + } |
| 93 | + if (this._.isObject(customProps) && this._.isArray(customProps.events) && customProps.events.length > 0 && this._.isObject(customProps.properties) && !this._.isEmptyObject(customProps.properties)) { |
| 94 | + this.customRegister.push(customProps); |
| 95 | + } else { |
| 96 | + this.log('RegisterProperties: register 参数错误'); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + RegisterProperties.prototype.hookRegister = function (customFun) { |
| 101 | + if (!this.sd) { |
| 102 | + this.log('神策JS SDK未成功引入'); |
| 103 | + return; |
| 104 | + } |
| 105 | + if (this._.isFunction(customFun)) { |
| 106 | + this.customRegister.push(customFun); |
| 107 | + } else { |
| 108 | + this.log('RegisterProperties: hookRegister 参数错误'); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + function Store(sd) { |
| 113 | + this.sd = sd; |
| 114 | + this._ = sd._; |
| 115 | + this.cookie_value = null; |
| 116 | + } |
| 117 | + Store.prototype.saveObjectVal = function (name, value) { |
| 118 | + if (!this._.isString(value)) { |
| 119 | + value = JSON.stringify(value); |
| 120 | + } |
| 121 | + if (this.sd.para.encrypt_cookie == true) { |
| 122 | + value = this._.encrypt(value); |
| 123 | + } |
| 124 | + if (this._.cookie.isSupport()) { |
| 125 | + this._.cookie.set(name, value); |
| 126 | + } |
| 127 | + this.cookie_value = value; |
| 128 | + }; |
| 129 | + |
| 130 | + Store.prototype.readObjectVal = function (name) { |
| 131 | + var value = this._.cookie.isSupport() ? this._.cookie.get(name) : this.cookie_value; |
| 132 | + if (!value) return null; |
| 133 | + value = this._.decryptIfNeeded(value); |
| 134 | + return this._.safeJSONParse(value); |
| 135 | + }; |
| 136 | + |
| 137 | + var COOKIE_NAME = 'sensorsdata2015jssdksession'; |
| 138 | + |
| 139 | + function SessionEvent() { |
| 140 | + this.registerProperties = null; |
| 141 | + this.store = null; |
| 142 | + this.sd = null; |
| 143 | + this._ = null; |
| 144 | + this.log = (window.console && window.console.log) || function () {}; |
| 145 | + this.cookie_name = ''; |
| 146 | + this.prop = {}; |
| 147 | + } |
| 148 | + |
| 149 | + SessionEvent.prototype.init = function (sd) { |
| 150 | + if (!sd || typeof sd !== 'object') { |
| 151 | + this.log('Session Event 插件初始化失败!'); |
| 152 | + return; |
| 153 | + } |
| 154 | + var _this = this; |
| 155 | + this.sd = sd; |
| 156 | + this._ = sd._; |
| 157 | + this.log = sd.log; |
| 158 | + this.cookie_name = COOKIE_NAME + (sd.para.sdk_id || ''); |
| 159 | + this.registerProperties = new RegisterProperties(); |
| 160 | + this.registerProperties.init(sd); |
| 161 | + this.store = new Store(sd); |
| 162 | + this.registerProperties.hookRegister(function () { |
| 163 | + return _this.addSessionID(); |
| 164 | + }); |
| 165 | + }; |
| 166 | + |
| 167 | + SessionEvent.prototype.addSessionID = function () { |
| 168 | + var time = +new Date(); |
| 169 | + this.prop = this.store.readObjectVal(this.cookie_name) || {}; |
| 170 | + |
| 171 | + var first_time = this.prop.first_session_time; |
| 172 | + var latest_time = this.prop.latest_session_time; |
| 173 | + // 切换逻辑:首次 || 向前调整时间 || 首次时间与当前时间差大于 12 小时 || 当前时间与上次触发时间大于30分钟 |
| 174 | + if (!first_time || !latest_time || first_time > time || latest_time > time || time - first_time > 12 * 60 * 60 * 1000 || time - latest_time > 30 * 60 * 1000) { |
| 175 | + var uuid = this._.UUID(); |
| 176 | + this.prop = { |
| 177 | + session_id: uuid.replace(/-/g, ''), |
| 178 | + first_session_time: time, |
| 179 | + latest_session_time: time |
| 180 | + }; |
| 181 | + } else { |
| 182 | + this.prop.latest_session_time = time; |
| 183 | + } |
| 184 | + |
| 185 | + this.store.saveObjectVal(this.cookie_name, this.prop); |
| 186 | + return { |
| 187 | + $event_session_id: this.prop.session_id |
| 188 | + }; |
| 189 | + }; |
| 190 | + |
| 191 | + var instance = new SessionEvent(); |
| 192 | + instance.__constructor__ = SessionEvent; |
| 193 | + // 对外公开 PageLoad 插件 |
| 194 | + if (window.SensorsDataWebJSSDKPlugin && Object.prototype.toString.call(window.SensorsDataWebJSSDKPlugin) === '[object Object]') { |
| 195 | + window.SensorsDataWebJSSDKPlugin.SessionEvent = window.SensorsDataWebJSSDKPlugin.SessionEvent || instance; |
| 196 | + } else { |
| 197 | + window.SensorsDataWebJSSDKPlugin = { |
| 198 | + SessionEvent: instance |
| 199 | + }; |
| 200 | + } |
| 201 | + |
| 202 | + return instance; |
| 203 | + |
| 204 | +}()); |
0 commit comments