Skip to content

Commit

Permalink
move parts around
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan-mitra authored and prabrishac committed Jan 29, 2021
1 parent cc1f5bc commit 66132f8
Showing 1 changed file with 34 additions and 35 deletions.
69 changes: 34 additions & 35 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import {
CONFIG_URL,
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL,
INTEGRATION_LOAD_CHECK_INTERVAL
} from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
Expand All @@ -42,7 +42,7 @@ import parseLinker from "./utils/linker";

const queryDefaults = {
trait: "ajs_trait_",
prop: "ajs_prop_",
prop: "ajs_prop_"
};

// https://unpkg.com/[email protected]/dist/browser.js
Expand Down Expand Up @@ -90,7 +90,7 @@ class Analytics {
this.readyCallback = () => {};
this.executeReadyCallback = undefined;
this.methodToCallbackMapping = {
syncPixel: "syncPixelCallback",
syncPixel: "syncPixelCallback"
};
this.loaded = false;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ class Analytics {
if (destination.enabled) {
this.clientIntegrations.push({
name: destination.destinationDefinition.name,
config: destination.config,
config: destination.config
});
}
}, this);
Expand All @@ -165,7 +165,7 @@ class Analytics {
);

// remove from the list which don't have support yet in SDK
this.clientIntegrations = this.clientIntegrations.filter((intg) => {
this.clientIntegrations = this.clientIntegrations.filter(intg => {
return integrations[intg.name] != undefined;
});

Expand Down Expand Up @@ -204,8 +204,7 @@ class Analytics {
this.toBeProcessedByIntegrationArray = [];
return;
}

intgArray.forEach((intg) => {
intgArray.forEach(intg => {
try {
logger.debug(
"[Analytics] init :: trying to initialize integration name:: ",
Expand Down Expand Up @@ -233,7 +232,8 @@ class Analytics {
if (
object.successfullyLoadedIntegration.length +
object.failedToBeLoadedIntegration.length ===
object.clientIntegrations.length && !object.areEventsReplayed
object.clientIntegrations.length &&
!object.areEventsReplayed
) {
logger.debug(
"===replay events called====",
Expand All @@ -257,7 +257,7 @@ class Analytics {
logger.debug("==registering ready callback===");
object.on("ready", object.executeReadyCallback);

object.clientIntegrationObjects.forEach((intg) => {
object.clientIntegrationObjects.forEach(intg => {
logger.debug("===looping over each successful integration====");
if (!intg.isReady || intg.isReady()) {
logger.debug("===letting know I am ready=====", intg.name);
Expand All @@ -267,7 +267,7 @@ class Analytics {

if (object.toBeProcessedByIntegrationArray.length > 0) {
// send the queued events to the fetched integration
object.toBeProcessedByIntegrationArray.forEach((event) => {
object.toBeProcessedByIntegrationArray.forEach(event => {
const methodName = event[0];
event.shift();

Expand Down Expand Up @@ -316,19 +316,19 @@ class Analytics {
}
});
object.toBeProcessedByIntegrationArray = [];
object.areEventsReplayed = true;
}
object.areEventsReplayed = true;
}
}

pause(time) {
return new Promise((resolve) => {
return new Promise(resolve => {
setTimeout(resolve, time);
});
}

isInitialized(instance, time = 0) {
return new Promise((resolve) => {
return new Promise(resolve => {
if (instance.isLoaded()) {
logger.debug("===integration loaded successfully====", instance.name);
this.successfullyLoadedIntegration.push(instance);
Expand Down Expand Up @@ -585,7 +585,7 @@ class Analytics {
rudderElement.message.context.traits
) {
this.userTraits = {
...rudderElement.message.context.traits,
...rudderElement.message.context.traits
};
this.storage.setUserTraits(this.userTraits);
}
Expand Down Expand Up @@ -648,7 +648,7 @@ class Analytics {
// rudderElement.message.context.page = getDefaultPageProperties();

rudderElement.message.context.traits = {
...this.userTraits,
...this.userTraits
};

logger.debug("anonymousId: ", this.anonymousId);
Expand All @@ -663,7 +663,7 @@ class Analytics {
}
if (this.groupTraits) {
rudderElement.message.traits = {
...this.groupTraits,
...this.groupTraits
};
}
}
Expand Down Expand Up @@ -691,7 +691,7 @@ class Analytics {

// try to first send to all integrations, if list populated from BE
try {
succesfulLoadedIntersectClientSuppliedIntegrations.forEach((obj) => {
succesfulLoadedIntersectClientSuppliedIntegrations.forEach(obj => {
if (!obj.isFailed || !obj.isFailed()) {
if (obj[type]) {
obj[type](rudderElement);
Expand Down Expand Up @@ -764,18 +764,18 @@ class Analytics {
const toplevelElements = [
"integrations",
"anonymousId",
"originalTimestamp",
"originalTimestamp"
];
for (const key in options) {
if (toplevelElements.includes(key)) {
rudderElement.message[key] = options[key];
} else if (key !== "context") {
rudderElement.message.context = merge(rudderElement.message.context, {
[key]: options[key],
[key]: options[key]
});
} else if (typeof options[key] === "object" && options[key] != null) {
rudderElement.message.context = merge(rudderElement.message.context, {
...options[key],
...options[key]
});
} else {
logger.error(
Expand Down Expand Up @@ -886,7 +886,7 @@ class Analytics {
if (!this.isValidWriteKey(writeKey) || !this.isValidServerUrl(serverUrl)) {
handleError({
message:
"[Analytics] load:: Unable to load due to wrong writeKey or serverUrl",
"[Analytics] load:: Unable to load due to wrong writeKey or serverUrl"
});
throw Error("failed to initialize");
}
Expand All @@ -911,7 +911,7 @@ class Analytics {
if (options && options.clientSuppliedCallbacks) {
// convert to rudder recognised method names
const tranformedCallbackMapping = {};
Object.keys(this.methodToCallbackMapping).forEach((methodName) => {
Object.keys(this.methodToCallbackMapping).forEach(methodName => {
if (this.methodToCallbackMapping.hasOwnProperty(methodName)) {
if (
options.clientSuppliedCallbacks[
Expand Down Expand Up @@ -985,7 +985,7 @@ class Analytics {
}

initializeCallbacks() {
Object.keys(this.methodToCallbackMapping).forEach((methodName) => {
Object.keys(this.methodToCallbackMapping).forEach(methodName => {
if (this.methodToCallbackMapping.hasOwnProperty(methodName)) {
this.on(methodName, () => {});
}
Expand All @@ -994,7 +994,7 @@ class Analytics {

registerCallbacks(calledFromLoad) {
if (!calledFromLoad) {
Object.keys(this.methodToCallbackMapping).forEach((methodName) => {
Object.keys(this.methodToCallbackMapping).forEach(methodName => {
if (this.methodToCallbackMapping.hasOwnProperty(methodName)) {
if (window.rudderanalytics) {
if (
Expand Down Expand Up @@ -1023,7 +1023,7 @@ class Analytics {
});
}

Object.keys(this.clientSuppliedCallbacks).forEach((methodName) => {
Object.keys(this.clientSuppliedCallbacks).forEach(methodName => {
if (this.clientSuppliedCallbacks.hasOwnProperty(methodName)) {
logger.debug(
"registerCallbacks",
Expand All @@ -1049,7 +1049,7 @@ class Analytics {
parseQueryString(query) {
function getTraitsFromQueryObject(qObj) {
const traits = {};
Object.keys(qObj).forEach((key) => {
Object.keys(qObj).forEach(key => {
if (key.substr(0, queryDefaults.trait.length) == queryDefaults.trait) {
traits[key.substr(queryDefaults.trait.length)] = qObj[key];
}
Expand All @@ -1060,7 +1060,7 @@ class Analytics {

function getEventPropertiesFromQueryObject(qObj) {
const props = {};
Object.keys(qObj).forEach((key) => {
Object.keys(qObj).forEach(key => {
if (key.substr(0, queryDefaults.prop.length) == queryDefaults.prop) {
props[key.substr(queryDefaults.prop.length)] = qObj[key];
}
Expand Down Expand Up @@ -1090,7 +1090,7 @@ class Analytics {
}

function pushQueryStringDataToAnalyticsArray(argumentsArray, obj) {
if(!argumentsArray){
if (!argumentsArray) {
argumentsArray = [];
}
if (obj.anonymousId) {
Expand All @@ -1111,16 +1111,16 @@ function pushQueryStringDataToAnalyticsArray(argumentsArray, obj) {
}
}

function processDataInAnalyticsArray(analytics){
if(instance.loaded){
function processDataInAnalyticsArray(analytics) {
if (instance.loaded) {
for (let i = 0; i < analytics.toBeProcessedArray.length; i++) {
const event = [...analytics.toBeProcessedArray[i]];
const method = event[0];
event.shift();
logger.debug("=====from analytics array, calling method:: ", method);
analytics[method](...event);
}

instance.toBeProcessedArray = [];
}
}
Expand All @@ -1131,7 +1131,7 @@ Emitter(instance);

window.addEventListener(
"error",
(e) => {
e => {
handleError(e, instance);
},
true
Expand Down Expand Up @@ -1173,12 +1173,11 @@ const parsedQueryObject = instance.parseQueryString(window.location.search);
pushQueryStringDataToAnalyticsArray(argumentsArray, parsedQueryObject);

if (eventsPushedAlready && argumentsArray && argumentsArray.length > 0) {
for (let i = 0; i < argumentsArray.length; i++) {
for (let i = 0; i < argumentsArray.length; i++) {
instance.toBeProcessedArray.push(argumentsArray[i]);
}

processDataInAnalyticsArray(instance);

}
// }

Expand All @@ -1205,5 +1204,5 @@ export {
alias,
group,
getAnonymousId,
setAnonymousId,
setAnonymousId
};

0 comments on commit 66132f8

Please sign in to comment.