Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow API calls before load #507

Merged
merged 10 commits into from
Apr 29, 2022
Merged
31 changes: 22 additions & 9 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,21 +1336,34 @@ const eventsPushedAlready =
!!window.rudderanalytics &&
window.rudderanalytics.push == Array.prototype.push;

const argumentsArray = window.rudderanalytics;

while (argumentsArray && argumentsArray[0] && argumentsArray[0][0] !== "load") {
argumentsArray.shift();
const argumentsArray = window.rudderanalytics || [];
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
/**
* Iterate the call stack until we find load call and
* then shift it to the beginning.
*
* Ex: Say the call stack currently have [page, identify, load, track]
* It will become [load, page, identify, track]
*/
let i = 0;
while (i < argumentsArray.length) {
if (argumentsArray[i] && argumentsArray[i][0] === "load") {
argumentsArray.unshift(argumentsArray.splice(i, 1)[0]);
break;
}
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
i += 1;
}
if (
argumentsArray &&
argumentsArray.length > 0 &&
argumentsArray[0][0] === "load"
) {

if (argumentsArray.length > 0 && argumentsArray[0][0] === "load") {
const method = argumentsArray[0][0];
argumentsArray[0].shift();
logger.debug("=====from init, calling method:: ", method);
instance[method](...argumentsArray[0]);
argumentsArray.shift();
} else {
/**
* When load is not present in the call stack restrict other events from proceeding
*/
argumentsArray.length = 0;
}
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved

// once loaded, parse querystring of the page url to send events
Expand Down