Skip to content

Commit

Permalink
Add "getSourceConfig" option as an alternative to "configUrl" to get …
Browse files Browse the repository at this point in the history
…sourceConfig
  • Loading branch information
gassiss committed Feb 22, 2021
1 parent f071344 commit 49211f7
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class Analytics {
processResponse(status, response) {
try {
logger.debug(`===in process response=== ${status}`);
response = JSON.parse(response);
if (typeof response === 'string') {
response = JSON.parse(response);
}
if (
response.source.useAutoTracking &&
!this.autoTrackHandlersRegistered
Expand Down Expand Up @@ -978,14 +980,38 @@ class Analytics {
);
}
}
try {
getJSONTrimmed(this, configUrl, writeKey, this.processResponse);
} catch (error) {

function errorHandler(error) {
handleError(error);
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
addDomEventHandlers(this);
}
}

if (options && options.getSourceConfig) {
if (typeof options.getSourceConfig !== "function") {
handleError('option "getSourceConfig" must be a function');
} else {
const res = options.getSourceConfig();

if (res instanceof Promise) {
res
.then(res => this.processResponse(200, res))
.catch(errorHandler)
} else {
this.processResponse(200, res);
}

processDataInAnalyticsArray(this);
}
return;
}

try {
getJSONTrimmed(this, configUrl, writeKey, this.processResponse);
} catch (error) {
errorHandler(error)
}
processDataInAnalyticsArray(this);
}

Expand Down

0 comments on commit 49211f7

Please sign in to comment.