|
1966 | 1966 | } |
1967 | 1967 | }; |
1968 | 1968 |
|
| 1969 | + _.eventEmitter = function() { |
| 1970 | + this._events = []; |
| 1971 | + this.pendingEvents = []; |
| 1972 | + } |
1969 | 1973 |
|
| 1974 | + _.eventEmitter.prototype = { |
| 1975 | + emit: function(type) { |
| 1976 | + var args = [].slice.call(arguments, 1); |
| 1977 | + |
| 1978 | + _.each(this._events, function(val) { |
| 1979 | + if (val.type !== type) { |
| 1980 | + return; |
| 1981 | + } |
| 1982 | + val.callback.apply(val.context, args); |
| 1983 | + }) |
| 1984 | + }, |
| 1985 | + on: function(event, callback, context) { |
| 1986 | + if (typeof callback !== 'function') { |
| 1987 | + return; |
| 1988 | + } |
| 1989 | + this._events.push({ |
| 1990 | + type: event, |
| 1991 | + callback: callback, |
| 1992 | + context: context || this |
| 1993 | + }); |
| 1994 | + }, |
| 1995 | + tempAdd: function(event, data) { |
| 1996 | + if (!data || !event) { |
| 1997 | + return; |
| 1998 | + } |
| 1999 | + |
| 2000 | + this.pendingEvents.push({ |
| 2001 | + type: event, |
| 2002 | + data: data |
| 2003 | + }); |
| 2004 | + this.pendingEvents.length > 20 ? this.pendingEvents.shift() : null; |
| 2005 | + }, |
| 2006 | + isReady: function() { |
| 2007 | + var that = this; |
| 2008 | + this.tempAdd = this.emit; |
| 2009 | + |
| 2010 | + if (this.pendingEvents.length === 0) { |
| 2011 | + return; |
| 2012 | + } |
| 2013 | + _.each(this.pendingEvents, function(val) { |
| 2014 | + that.emit(val.type, val.data); |
| 2015 | + }) |
| 2016 | + |
| 2017 | + this.pendingEvents = []; |
| 2018 | + |
| 2019 | + } |
| 2020 | + |
| 2021 | + } |
1970 | 2022 |
|
1971 | 2023 |
|
1972 | 2024 | })(); |
|
2067 | 2119 | } |
2068 | 2120 | } |
2069 | 2121 | if (typeof sd.para.server_url === 'string' && sd.para.server_url.slice(0, 3) === '://') { |
2070 | | - sd.para.server_url = location.protocol + sd.para.server_url; |
| 2122 | + sd.para.server_url = location.protocol.slice(-1) + sd.para.server_url; |
2071 | 2123 | } |
2072 | 2124 | if (typeof sd.para.web_url === 'string' && sd.para.web_url.slice(0, 3) === '://') { |
2073 | | - sd.para.web_url = location.protocol + sd.para.web_url; |
| 2125 | + sd.para.web_url = location.protocol.slice(-1) + sd.para.web_url; |
2074 | 2126 | } |
2075 | 2127 |
|
2076 | 2128 | if (sd.para.send_type !== 'image' && sd.para.send_type !== 'ajax' && sd.para.send_type !== 'beacon') { |
|
2183 | 2235 |
|
2184 | 2236 | sd.setInitVar = function() { |
2185 | 2237 | sd._t = sd._t || 1 * new Date(); |
2186 | | - sd.lib_version = '1.14.24'; |
| 2238 | + sd.lib_version = '1.15.1'; |
2187 | 2239 | sd.is_first_visitor = false; |
2188 | 2240 | sd.source_channel_standard = 'utm_source utm_medium utm_campaign utm_content utm_term'; |
2189 | 2241 | }; |
|
2453 | 2505 | } else { |
2454 | 2506 | return sd.store._state._first_id || sd.store._state.first_id || sd.store._state._distinct_id || sd.store._state.distinct_id; |
2455 | 2507 | } |
| 2508 | + }, |
| 2509 | + setPlugin: function(para) { |
| 2510 | + if (!_.isObject(para)) { |
| 2511 | + return false; |
| 2512 | + } |
| 2513 | + _.each(para, function(v, k) { |
| 2514 | + if (_.isFunction(v)) { |
| 2515 | + if (_.isObject(window.SensorsDataWebJSSDKPlugin) && window.SensorsDataWebJSSDKPlugin[k]) { |
| 2516 | + v(window.SensorsDataWebJSSDKPlugin[k]); |
| 2517 | + } else { |
| 2518 | + sd.log(k + '没有获取到,请查阅文档,调整' + k + '的引入顺序!') |
| 2519 | + } |
| 2520 | + } |
| 2521 | + }); |
2456 | 2522 | } |
2457 | | - |
2458 | | - |
2459 | 2523 | }; |
2460 | 2524 |
|
2461 | 2525 | sd.quick = function() { |
|
2630 | 2694 | } |
2631 | 2695 | var firstId = store.getFirstId(); |
2632 | 2696 | if (typeof id === 'undefined') { |
| 2697 | + var uuid = _.UUID(); |
2633 | 2698 | if (firstId) { |
2634 | | - store.set('first_id', _.UUID()); |
| 2699 | + store.set('first_id', uuid); |
2635 | 2700 | } else { |
2636 | | - store.set('distinct_id', _.UUID()); |
| 2701 | + store.set('distinct_id', uuid); |
2637 | 2702 | } |
2638 | 2703 | } else if (saEvent.check({ |
2639 | 2704 | distinct_id: id |
|
2758 | 2823 | if (firstId) { |
2759 | 2824 | store.set('first_id', ''); |
2760 | 2825 | if (isChangeId === true) { |
2761 | | - store.set('distinct_id', _.UUID()); |
| 2826 | + var uuid = _.UUID(); |
| 2827 | + store.set('distinct_id', uuid); |
2762 | 2828 | } else { |
2763 | 2829 | store.set('distinct_id', firstId); |
2764 | 2830 | } |
| 2831 | + |
2765 | 2832 | } else { |
2766 | 2833 | sd.log('没有first_id,logout失败'); |
2767 | 2834 | } |
|
2798 | 2865 |
|
2799 | 2866 |
|
2800 | 2867 |
|
| 2868 | + |
| 2869 | + |
| 2870 | + |
| 2871 | + |
| 2872 | + |
| 2873 | + |
| 2874 | + |
| 2875 | + |
| 2876 | + |
| 2877 | + |
2801 | 2878 | function BatchSend() { |
2802 | 2879 | this.sendingData = 0; |
2803 | 2880 | }; |
|
3093 | 3170 |
|
3094 | 3171 | var sendState = {}; |
3095 | 3172 | sd.sendState = sendState; |
| 3173 | + sd.events = new _.eventEmitter(); |
3096 | 3174 | sendState.queue = _.autoExeQueue(); |
3097 | 3175 |
|
3098 | 3176 | sendState.requestData = null; |
|
3122 | 3200 | callback: callback |
3123 | 3201 | }; |
3124 | 3202 |
|
| 3203 | + sd.events.tempAdd('send', originData); |
| 3204 | + |
3125 | 3205 | if (!sd.para.use_app_track && sd.para.batch_send && localStorage.length < 200) { |
3126 | 3206 | sd.log(originData); |
3127 | 3207 | sd.batchSend.add(this.requestData.data); |
|
3517 | 3597 | }, |
3518 | 3598 | set: function(name, value) { |
3519 | 3599 | this._state = this._state || {}; |
| 3600 | + if (name === 'distinct_id' && this._state.distinct_id) { |
| 3601 | + sd.events.tempAdd('changeDistinctId', value); |
| 3602 | + } |
3520 | 3603 | this._state[name] = value; |
3521 | 3604 | if (name === 'first_id') { |
3522 | 3605 | delete this._state._first_id; |
3523 | 3606 | } else if (name === 'distinct_id') { |
3524 | 3607 | delete this._state._distinct_id; |
3525 | 3608 | } |
3526 | 3609 | this.save(); |
| 3610 | + |
3527 | 3611 | }, |
3528 | 3612 | change: function(name, value) { |
3529 | 3613 | this._state['_' + name] = value; |
|
3789 | 3873 | sd.errorMsg = '您SDK没有配置开启点击图,可能没有数据!'; |
3790 | 3874 | } |
3791 | 3875 | if (web_url && web_url[0] && web_url[1]) { |
3792 | | - if (web_url[1].slice(0, 5) === 'http:' && location.protocol === 'https') { |
| 3876 | + if (web_url[1].slice(0, 5) === 'http:' && location.protocol === 'https:') { |
3793 | 3877 | sd.errorMsg = '您的当前页面是https的地址,神策分析环境也必须是https!'; |
3794 | 3878 | } |
3795 | 3879 | } |
|
4278 | 4362 | if (typeof window['sensorsDataAnalytic201505'] === 'string') { |
4279 | 4363 | sd.setPreConfig(window[sensorsDataAnalytic201505]); |
4280 | 4364 | window[sensorsDataAnalytic201505] = sd; |
4281 | | - sd.init(); |
4282 | 4365 | window['sensorsDataAnalytic201505'] = sd; |
| 4366 | + sd.init(); |
4283 | 4367 | } else if (typeof window['sensorsDataAnalytic201505'] === 'undefined') { |
4284 | 4368 | window['sensorsDataAnalytic201505'] = sd; |
4285 | 4369 | return sd; |
|
0 commit comments