Skip to content

Commit f26c708

Browse files
committed
修改子域cookie 问题
1 parent 5026b0e commit f26c708

File tree

5 files changed

+277
-249
lines changed

5 files changed

+277
-249
lines changed

sensorsdata.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sdk.js

Lines changed: 137 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,141 +1474,155 @@ saEvent.send = function(p, callback) {
14741474
};
14751475

14761476
var store = sd.store = {
1477-
_sessionState: {},
1478-
_state: {},
1479-
getProps: function() {
1480-
return this._state.props;
1481-
},
1482-
getSessionProps: function() {
1483-
return this._sessionState;
1484-
},
1485-
getDistinctId: function() {
1486-
return this._state.distinct_id;
1487-
},
1488-
getFirstId: function(){
1489-
return this._state.first_id;
1490-
},
1491-
toState: function(ds) {
1492-
var state = null;
1493-
if (ds != null && _.isJSONString(ds)) {
1494-
state = JSON.parse(ds);
1495-
if (state.distinct_id) {
1496-
this._state = state;
1497-
1498-
if(typeof(state.props) === 'object'){
1499-
for(var key in state.props){
1500-
if(typeof state.props[key] === 'string'){
1501-
state.props[key] = state.props[key].slice(0, sd.para.max_referrer_string_length);
1477+
_sessionState: {},
1478+
_state: {},
1479+
getProps: function() {
1480+
return this._state.props;
1481+
},
1482+
getSessionProps: function() {
1483+
return this._sessionState;
1484+
},
1485+
getDistinctId: function() {
1486+
return this._state.distinct_id;
1487+
},
1488+
getFirstId: function(){
1489+
return this._state.first_id;
1490+
},
1491+
toState: function(ds) {
1492+
var state = null;
1493+
if (ds != null && _.isJSONString(ds)) {
1494+
state = JSON.parse(ds);
1495+
if (state.distinct_id) {
1496+
this._state = state;
1497+
1498+
if(typeof(state.props) === 'object'){
1499+
for(var key in state.props){
1500+
if(typeof state.props[key] === 'string'){
1501+
state.props[key] = state.props[key].slice(0, sd.para.max_referrer_string_length);
1502+
}
15021503
}
1504+
this.save();
15031505
}
1504-
this.save();
1505-
}
15061506

1507+
} else {
1508+
this.set('distinct_id', _.UUID());
1509+
error_msg.push('toStateParseDistinctError');
1510+
}
15071511
} else {
15081512
this.set('distinct_id', _.UUID());
1509-
error_msg.push('toStateParseDistinctError');
1513+
error_msg.push('toStateParseError');
15101514
}
1511-
} else {
1512-
this.set('distinct_id', _.UUID());
1513-
error_msg.push('toStateParseError');
1514-
}
1515-
},
1516-
initSessionState: function() {
1517-
var ds = _.cookie.get('sensorsdata2015session');
1518-
var state = null;
1519-
if (ds !== null && (typeof (state = JSON.parse(ds)) === 'object')) {
1520-
this._sessionState = state || {};
1521-
}
1522-
},
1515+
},
1516+
initSessionState: function() {
1517+
var ds = _.cookie.get('sensorsdata2015session');
1518+
var state = null;
1519+
if (ds !== null && (typeof (state = JSON.parse(ds)) === 'object')) {
1520+
this._sessionState = state || {};
1521+
}
1522+
},
15231523

1524-
setOnce: function(a, b) {
1525-
if (!(a in this._state)) {
1526-
this.set(a, b);
1527-
}
1528-
},
1529-
set: function(name, value) {
1530-
this._state = this._state || {};
1531-
this._state[name] = value;
1532-
this.save();
1533-
},
1534-
// 针对当前页面修改
1535-
change: function(name, value) {
1536-
this._state[name] = value;
1537-
},
1538-
setSessionProps: function(newp) {
1539-
var props = this._sessionState;
1540-
_.extend(props, newp);
1541-
this.sessionSave(props);
1542-
},
1543-
setSessionPropsOnce: function(newp) {
1544-
var props = this._sessionState;
1545-
_.coverExtend(props, newp);
1546-
this.sessionSave(props);
1547-
},
1548-
setProps: function(newp) {
1549-
var props = this._state.props || {};
1550-
_.extend(props, newp);
1551-
this.set('props', props);
1552-
},
1553-
setPropsOnce: function(newp) {
1554-
var props = this._state.props || {};
1555-
_.coverExtend(props, newp);
1556-
this.set('props', props);
1557-
},
1558-
clearAllProps: function() {
1559-
this._sessionState = {};
1560-
for(var i in this._state.props){
1561-
if(i.indexOf('latest_') !== 1){
1562-
delete this._state.props[i];
1524+
setOnce: function(a, b) {
1525+
if (!(a in this._state)) {
1526+
this.set(a, b);
15631527
}
1564-
}
1565-
this.sessionSave({});
1566-
this.save();
1567-
},
1568-
sessionSave: function(props) {
1569-
this._sessionState = props;
1570-
_.cookie.set('sensorsdata2015session', JSON.stringify(this._sessionState), 0);
1571-
},
1572-
save: function() {
1573-
_.cookie.set('sensorsdata2015jssdkcross', JSON.stringify(this._state), 730, sd.para.cross_subdomain);
1574-
},
1575-
init: function() {
1576-
// 如果不支持cookie,设置新的id,并且带有error_msg
1577-
if (!navigator.cookieEnabled) {
1578-
error_msg.push('cookieNotEnable');
1579-
if (!_.localStorage.isSupport) {
1580-
error_msg.push('localStorageNotEnable');
1528+
},
1529+
set: function(name, value) {
1530+
this._state = this._state || {};
1531+
this._state[name] = value;
1532+
this.save();
1533+
},
1534+
// 针对当前页面修改
1535+
change: function(name, value) {
1536+
this._state[name] = value;
1537+
},
1538+
setSessionProps: function(newp) {
1539+
var props = this._sessionState;
1540+
_.extend(props, newp);
1541+
this.sessionSave(props);
1542+
},
1543+
setSessionPropsOnce: function(newp) {
1544+
var props = this._sessionState;
1545+
_.coverExtend(props, newp);
1546+
this.sessionSave(props);
1547+
},
1548+
setProps: function(newp) {
1549+
var props = this._state.props || {};
1550+
_.extend(props, newp);
1551+
this.set('props', props);
1552+
},
1553+
setPropsOnce: function(newp) {
1554+
var props = this._state.props || {};
1555+
_.coverExtend(props, newp);
1556+
this.set('props', props);
1557+
},
1558+
clearAllProps: function() {
1559+
this._sessionState = {};
1560+
for(var i in this._state.props){
1561+
if(i.indexOf('latest_') !== 1){
1562+
delete this._state.props[i];
1563+
}
1564+
}
1565+
this.sessionSave({});
1566+
this.save();
1567+
},
1568+
sessionSave: function(props) {
1569+
this._sessionState = props;
1570+
_.cookie.set('sensorsdata2015session', JSON.stringify(this._sessionState), 0);
1571+
},
1572+
save: function() {
1573+
_.cookie.set(this.getCookieName(), JSON.stringify(this._state), 73000, sd.para.cross_subdomain);
1574+
},
1575+
getCookieName: function(){
1576+
var sub = '';
1577+
if(sd.para.cross_subdomain === false){
1578+
sub = _.url('sub',location.href);
1579+
if(typeof sub === 'string' && sub !== ''){
1580+
sub = 'sa_jssdk_2015_' + sub;
1581+
}else{
1582+
sub = 'sa_jssdk_2015_root_' + sub;
1583+
}
1584+
}else{
1585+
sub = 'sensorsdata2015jssdkcross';
1586+
}
1587+
return sub;
1588+
},
1589+
init: function() {
1590+
// 如果不支持cookie,设置新的id,并且带有error_msg
1591+
if (!navigator.cookieEnabled) {
1592+
error_msg.push('cookieNotEnable');
1593+
if (!_.localStorage.isSupport) {
1594+
error_msg.push('localStorageNotEnable');
1595+
}
15811596
}
1582-
}
1583-
this.initSessionState();
1584-
var cross = _.cookie.get(sd.para.cross_subdomain ? 'sensorsdata2015jssdkcross' : 'sensorsdata2015jssdk');
1585-
if (cross === null) {
1586-
// 判断是否是第一次载入sdk
1587-
is_first_visitor = true;
1588-
1589-
just_test_distinctid = 1;
1590-
1591-
this.set('distinct_id', _.UUID());
1592-
} else {
1593-
1594-
just_test_distinctid = 2;
1595-
just_test_distinctid_detail = JSON.stringify(cross);
1596-
just_test_distinctid_detail2 = navigator.userAgent+'^_^'+document.cookie;
15971597

1598-
this.toState(cross);
1599-
}
1600-
//判断新用户
1601-
saNewUser.storeInitCheck();
1602-
saNewUser.checkIsFirstLatest();
1603-
// 如果初始化cookie失败,发送错误事件
1604-
/*
1605-
if(error_msg.length > 0 && sd.para.send_error_event){
1606-
sd.track('jssdk_error_msg');
1607-
}*/
1598+
this.initSessionState();
1599+
var cross = _.cookie.get(this.getCookieName());
1600+
if (cross === null) {
1601+
// 判断是否是第一次载入sdk
1602+
is_first_visitor = true;
1603+
1604+
just_test_distinctid = 1;
1605+
1606+
this.set('distinct_id', _.UUID());
1607+
} else {
1608+
1609+
just_test_distinctid = 2;
1610+
just_test_distinctid_detail = JSON.stringify(cross);
1611+
just_test_distinctid_detail2 = navigator.userAgent+'^_^'+document.cookie;
16081612

1609-
}
1610-
};
1613+
this.toState(cross);
1614+
}
1615+
//判断新用户
1616+
saNewUser.storeInitCheck();
1617+
saNewUser.checkIsFirstLatest();
1618+
// 如果初始化cookie失败,发送错误事件
1619+
/*
1620+
if(error_msg.length > 0 && sd.para.send_error_event){
1621+
sd.track('jssdk_error_msg');
1622+
}*/
16111623

1624+
}
1625+
};
16121626
var commonWays = {
16131627
// 获取谷歌标准参数
16141628
getUtm: function() {

0 commit comments

Comments
 (0)