Skip to content

Commit 0d0090e

Browse files
author
shengyonggen
committed
login功能
1 parent 46be3fb commit 0d0090e

File tree

4 files changed

+109
-15
lines changed

4 files changed

+109
-15
lines changed

sensorsdata.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sdk.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,9 @@ saEvent.send = function(p, callback) {
13511351
getDistinctId: function() {
13521352
return this._state.distinct_id;
13531353
},
1354+
getFirstId: function(){
1355+
return this._state.first_id;
1356+
},
13541357
toState: function(ds) {
13551358
var state = null;
13561359
if (ds !== null && (typeof (state = JSON.parse(ds)) === 'object')) {
@@ -1666,14 +1669,26 @@ saEvent.send = function(p, callback) {
16661669
* @param {string} distinct_id
16671670
* */
16681671
sd.identify = function(id, isSave) {
1672+
var firstId = store.getFirstId();
16691673
if (typeof id === 'undefined') {
1670-
store.set('distinct_id', _.UUID());
1674+
if(firstId){
1675+
store.set('first_id', _.UUID());
1676+
}else{
1677+
store.set('distinct_id', _.UUID());
1678+
}
16711679
} else if (saEvent.check({distinct_id: id})) {
1672-
16731680
if (isSave === true) {
1674-
store.set('distinct_id', id);
1681+
if(firstId){
1682+
store.set('first_id', id);
1683+
}else{
1684+
store.set('distinct_id', id);
1685+
}
16751686
} else {
1676-
store.change('distinct_id', id);
1687+
if(firstId){
1688+
store.change('first_id', id);
1689+
}else{
1690+
store.change('distinct_id', id);
1691+
}
16771692
}
16781693

16791694
} else {
@@ -1689,7 +1704,7 @@ saEvent.send = function(p, callback) {
16891704
sd.trackSignup = function(id, e, p, c) {
16901705
if (saEvent.check({distinct_id: id, event: e, properties: p})) {
16911706
saEvent.send({
1692-
original_id: store.getDistinctId(),
1707+
original_id: store.getFirstId() || store.getDistinctId(),
16931708
distinct_id: id,
16941709
type: 'track_signup',
16951710
event: e,
@@ -1698,6 +1713,7 @@ saEvent.send = function(p, callback) {
16981713
store.set('distinct_id', id);
16991714
}
17001715
};
1716+
17011717
/*
17021718
* @param {string} testid
17031719
* @param {string} groupid
@@ -1755,6 +1771,37 @@ saEvent.send = function(p, callback) {
17551771
}
17561772
};
17571773

1774+
sd.login = function(id){
1775+
if (saEvent.check({distinct_id: id})) {
1776+
var firstId = store.getFirstId();
1777+
var distinctId = store.getDistinctId();
1778+
if(id !== distinctId){
1779+
if(firstId){
1780+
sd.trackSignup(id,'$SignUp');
1781+
}else{
1782+
store.set('first_id',distinctId);
1783+
sd.trackSignup(id,'$SignUp');
1784+
}
1785+
}
1786+
} else {
1787+
logger.info('login的参数必须是字符串');
1788+
}
1789+
};
1790+
1791+
sd.logout = function(isChangeId){
1792+
var firstId = store.getFirstId();
1793+
if(firstId){
1794+
store.set('first_id','');
1795+
if(isChangeId === true){
1796+
store.set('distinct_id',_.UUID());
1797+
}else{
1798+
store.set('distinct_id',firstId);
1799+
}
1800+
}else{
1801+
logger.info('没有first_id,logout失败');
1802+
}
1803+
};
1804+
17581805

17591806
function app_js_bridge(){
17601807
var app_info = null;

src/sensorsdata.full.js

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}
614614
, slice = ArrayProto.slice
615615
, toString = ObjProto.toString
616616
, hasOwnProperty = ObjProto.hasOwnProperty
617-
, LIB_VERSION = '1.6.7';
617+
, LIB_VERSION = '1.6.9';
618618

619619
sd.lib_version = LIB_VERSION;
620620

@@ -1955,6 +1955,9 @@ saEvent.send = function(p, callback) {
19551955
getDistinctId: function() {
19561956
return this._state.distinct_id;
19571957
},
1958+
getFirstId: function(){
1959+
return this._state.first_id;
1960+
},
19581961
toState: function(ds) {
19591962
var state = null;
19601963
if (ds !== null && (typeof (state = JSON.parse(ds)) === 'object')) {
@@ -2270,14 +2273,26 @@ saEvent.send = function(p, callback) {
22702273
* @param {string} distinct_id
22712274
* */
22722275
sd.identify = function(id, isSave) {
2276+
var firstId = store.getFirstId();
22732277
if (typeof id === 'undefined') {
2274-
store.set('distinct_id', _.UUID());
2278+
if(firstId){
2279+
store.set('first_id', _.UUID());
2280+
}else{
2281+
store.set('distinct_id', _.UUID());
2282+
}
22752283
} else if (saEvent.check({distinct_id: id})) {
2276-
22772284
if (isSave === true) {
2278-
store.set('distinct_id', id);
2285+
if(firstId){
2286+
store.set('first_id', id);
2287+
}else{
2288+
store.set('distinct_id', id);
2289+
}
22792290
} else {
2280-
store.change('distinct_id', id);
2291+
if(firstId){
2292+
store.change('first_id', id);
2293+
}else{
2294+
store.change('distinct_id', id);
2295+
}
22812296
}
22822297

22832298
} else {
@@ -2293,7 +2308,7 @@ saEvent.send = function(p, callback) {
22932308
sd.trackSignup = function(id, e, p, c) {
22942309
if (saEvent.check({distinct_id: id, event: e, properties: p})) {
22952310
saEvent.send({
2296-
original_id: store.getDistinctId(),
2311+
original_id: store.getFirstId() || store.getDistinctId(),
22972312
distinct_id: id,
22982313
type: 'track_signup',
22992314
event: e,
@@ -2302,6 +2317,7 @@ saEvent.send = function(p, callback) {
23022317
store.set('distinct_id', id);
23032318
}
23042319
};
2320+
23052321
/*
23062322
* @param {string} testid
23072323
* @param {string} groupid
@@ -2359,6 +2375,37 @@ saEvent.send = function(p, callback) {
23592375
}
23602376
};
23612377

2378+
sd.login = function(id){
2379+
if (saEvent.check({distinct_id: id})) {
2380+
var firstId = store.getFirstId();
2381+
var distinctId = store.getDistinctId();
2382+
if(id !== distinctId){
2383+
if(firstId){
2384+
sd.trackSignup(id,'$SignUp');
2385+
}else{
2386+
store.set('first_id',distinctId);
2387+
sd.trackSignup(id,'$SignUp');
2388+
}
2389+
}
2390+
} else {
2391+
logger.info('login的参数必须是字符串');
2392+
}
2393+
};
2394+
2395+
sd.logout = function(isChangeId){
2396+
var firstId = store.getFirstId();
2397+
if(firstId){
2398+
store.set('first_id','');
2399+
if(isChangeId === true){
2400+
store.set('distinct_id',_.UUID());
2401+
}else{
2402+
store.set('distinct_id',firstId);
2403+
}
2404+
}else{
2405+
logger.info('没有first_id,logout失败');
2406+
}
2407+
};
2408+
23622409

23632410
function app_js_bridge(){
23642411
var app_info = null;

vtrack.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)