Skip to content

Commit eb1ff28

Browse files
authored
Merge pull request #26 from rudderlabs/develop
Develop
2 parents 2219b7f + f0ead88 commit eb1ff28

File tree

24 files changed

+3744
-208
lines changed

24 files changed

+3744
-208
lines changed

analytics.js

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import RudderElementBuilder from "./utils/RudderElementBuilder";
1515
import Storage from "./utils/storage";
1616
import { EventRepository } from "./utils/EventRepository";
1717
import logger from "./utils/logUtil";
18+
import { addDomEventHandlers } from "./utils/autotrack.js";
1819

1920
//https://unpkg.com/[email protected]/dist/browser.js
2021

@@ -40,8 +41,11 @@ class Analytics {
4041
* @memberof Analytics
4142
*/
4243
constructor() {
44+
this.autoTrackHandlersRegistered = false;
45+
this.autoTrackFeatureEnabled = false;
4346
this.initialized = false;
4447
this.ready = false;
48+
this.trackValues = [];
4549
this.eventsBuffer = [];
4650
this.clientIntegrations = [];
4751
this.configArray = [];
@@ -73,25 +77,42 @@ class Analytics {
7377
* @memberof Analytics
7478
*/
7579
processResponse(status, response) {
76-
logger.debug("===in process response=== " + status);
77-
response = JSON.parse(response);
78-
response.source.destinations.forEach(function(destination, index) {
80+
try {
81+
logger.debug("===in process response=== " + status);
82+
response = JSON.parse(response);
83+
if (response.source.useAutoTracking) {
84+
this.autoTrackFeatureEnabled = true;
85+
addDomEventHandlers(this);
86+
this.autoTrackHandlersRegistered = true;
87+
}
88+
response.source.destinations.forEach(function(destination, index) {
89+
logger.debug(
90+
"Destination " +
91+
index +
92+
" Enabled? " +
93+
destination.enabled +
94+
" Type: " +
95+
destination.destinationDefinition.name +
96+
" Use Native SDK? " +
97+
destination.config.useNativeSDK
98+
);
99+
if (destination.enabled && destination.config.useNativeSDK) {
100+
this.clientIntegrations.push(destination.destinationDefinition.name);
101+
this.configArray.push(destination.config);
102+
}
103+
}, this);
104+
this.init(this.clientIntegrations, this.configArray);
105+
} catch (error) {
106+
handleError(error);
107+
logger.debug("===handling config BE response processing error===");
79108
logger.debug(
80-
"Destination " +
81-
index +
82-
" Enabled? " +
83-
destination.enabled +
84-
" Type: " +
85-
destination.destinationDefinition.name +
86-
" Use Native SDK? " +
87-
destination.config.useNativeSDK
109+
"autoTrackHandlersRegistered",
110+
this.autoTrackHandlersRegistered
88111
);
89-
if (destination.enabled) {
90-
this.clientIntegrations.push(destination.destinationDefinition.name);
91-
this.configArray.push(destination.config);
112+
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
113+
addDomEventHandlers(this);
92114
}
93-
}, this);
94-
this.init(this.clientIntegrations, this.configArray);
115+
}
95116
}
96117

97118
/**
@@ -144,7 +165,12 @@ class Analytics {
144165
integrationOptions["All"])
145166
) {
146167
try {
147-
object.clientIntegrationObjects[i][methodName](...event);
168+
if (
169+
!object.clientIntegrationObjects[i]["isFailed"] ||
170+
!object.clientIntegrationObjects[i]["isFailed"]()
171+
) {
172+
object.clientIntegrationObjects[i][methodName](...event);
173+
}
148174
} catch (error) {
149175
handleError(error);
150176
}
@@ -243,6 +269,32 @@ class Analytics {
243269
this.processIdentify(userId, traits, options, callback);
244270
}
245271

272+
/**
273+
*
274+
* @param {*} to
275+
* @param {*} from
276+
* @param {*} options
277+
* @param {*} callback
278+
*/
279+
alias(to, from, options, callback) {
280+
if (typeof options == "function") (callback = options), (options = null);
281+
if (typeof from == "function")
282+
(callback = from), (options = null), (from = null);
283+
if (typeof from == "object") (options = from), (from = null);
284+
285+
let rudderElement = new RudderElementBuilder().setType("alias").build();
286+
rudderElement.message.previousId =
287+
from || this.userId ? this.userId : this.getAnonymousId();
288+
rudderElement.message.userId = to;
289+
290+
this.processAndSendDataToDestinations(
291+
"alias",
292+
rudderElement,
293+
options,
294+
callback
295+
);
296+
}
297+
246298
/**
247299
* Send page call to Rudder BE and to initialized integrations
248300
*
@@ -409,7 +461,9 @@ class Analytics {
409461
);
410462
logger.debug("anonymousId: ", this.anonymousId);
411463
rudderElement["message"]["anonymousId"] = this.anonymousId;
412-
rudderElement["message"]["userId"] = this.userId;
464+
rudderElement["message"]["userId"] = rudderElement["message"]["userId"]
465+
? rudderElement["message"]["userId"]
466+
: this.userId;
413467

414468
if (options) {
415469
this.processOptionsParam(rudderElement, options);
@@ -426,7 +480,9 @@ class Analytics {
426480
integrations[obj.name] ||
427481
(integrations[obj.name] == undefined && integrations["All"])
428482
) {
429-
obj[type](rudderElement);
483+
if (!obj["isFailed"] || !obj["isFailed"]()) {
484+
obj[type](rudderElement);
485+
}
430486
}
431487
});
432488
}
@@ -498,15 +554,15 @@ class Analytics {
498554
this.storage.clear();
499555
}
500556

501-
getAnonymousId(){
557+
getAnonymousId() {
502558
this.anonymousId = this.storage.getAnonymousId();
503-
if(!this.anonymousId){
559+
if (!this.anonymousId) {
504560
this.setAnonymousId();
505561
}
506562
return this.anonymousId;
507563
}
508564

509-
setAnonymousId(anonymousId){
565+
setAnonymousId(anonymousId) {
510566
this.anonymousId = anonymousId ? anonymousId : generateUUID();
511567
this.storage.setAnonymousId(this.anonymousId);
512568
}
@@ -527,12 +583,26 @@ class Analytics {
527583
if (options && options.logLevel) {
528584
logger.setLogLevel(options.logLevel);
529585
}
586+
if (
587+
options &&
588+
options.valTrackingList &&
589+
options.valTrackingList.push == Array.prototype.push
590+
) {
591+
this.trackValues = options.valTrackingList;
592+
}
530593
logger.debug("inside load ");
531594
this.eventRepository.writeKey = writeKey;
532595
if (serverUrl) {
533596
this.eventRepository.url = serverUrl;
534597
}
535-
getJSONTrimmed(this, CONFIG_URL, writeKey, this.processResponse);
598+
try {
599+
getJSONTrimmed(this, CONFIG_URL, writeKey, this.processResponse);
600+
} catch (error) {
601+
handleError(error);
602+
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
603+
addDomEventHandlers(instance);
604+
}
605+
}
536606
}
537607
}
538608

@@ -578,10 +648,21 @@ if (process.browser) {
578648
let identify = instance.identify.bind(instance);
579649
let page = instance.page.bind(instance);
580650
let track = instance.track.bind(instance);
651+
let alias = instance.alias.bind(instance);
581652
let reset = instance.reset.bind(instance);
582653
let load = instance.load.bind(instance);
583654
let initialized = (instance.initialized = true);
584655
let getAnonymousId = instance.getAnonymousId.bind(instance);
585656
let setAnonymousId = instance.setAnonymousId.bind(instance);
586657

587-
export { initialized, page, track, load, identify, reset, getAnonymousId, setAnonymousId };
658+
export {
659+
initialized,
660+
page,
661+
track,
662+
load,
663+
identify,
664+
reset,
665+
alias,
666+
getAnonymousId,
667+
setAnonymousId
668+
};

0 commit comments

Comments
 (0)