Skip to content

Commit

Permalink
replay events to sdks after successful intialization
Browse files Browse the repository at this point in the history
  • Loading branch information
prabrisha-rudder committed Oct 22, 2019
1 parent 00672ac commit 4712add
Show file tree
Hide file tree
Showing 8 changed files with 1,671 additions and 75 deletions.
85 changes: 56 additions & 29 deletions rudder-client-javascript/analytics/analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getJSONTrimmed, generateUUID, handleError } from "./utils/utils";
import { CONFIG_URL, ECommerceEvents } from "./utils/constants";
import { CONFIG_URL, ECommerceEvents, MAX_WAIT_FOR_INTEGRATION_LOAD, INTEGRATION_LOAD_CHECK_INTERVAL } from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
import { RudderTraits } from "./utils/RudderTraits";
Expand Down Expand Up @@ -28,7 +28,7 @@ function enqueue(rudderElement) {
*/
class Analytics {
/**
*Creates an instance of Analytics.
* Creates an instance of Analytics.
* @memberof Analytics
*/
constructor() {
Expand All @@ -37,6 +37,8 @@ class Analytics {
this.clientIntegrations = [];
this.configArray = [];
this.clientIntegrationObjects = undefined;
this.successfullyLoadedIntegration = [];
this.failedToBeLoadedIntegration = [];
this.toBeProcessedArray = [];
this.toBeProcessedByIntegrationArray = [];
this.storage = new Storage();
Expand Down Expand Up @@ -109,47 +111,72 @@ class Analytics {
let intgInstance = new intgClass(hubId);
intgInstance.init();

this.clientIntegrationObjects.push(intgInstance);
//this.clientIntegrationObjects.push(intgInstance);
this.isInitialized(intgInstance).then(this.replayEvents);
}
if (intg === "GA") {
let trackingID = configArray[i].trackingID;
let intgInstance = new intgClass(trackingID);
intgInstance.init();

this.clientIntegrationObjects.push(intgInstance);
//this.clientIntegrationObjects.push(intgInstance);
this.isInitialized(intgInstance).then(this.replayEvents);
}
});

}

// Add GA forcibly for tests , TODO : Remove
/* let GAClass = integrations["GA"];
let GAInstance = new GAClass("UA-143161493-8");
GAInstance.init();
console.log("GA initialized");
this.clientIntegrationObjects.push(GAInstance); */

//send the queued events to the fetched integration
this.toBeProcessedByIntegrationArray.forEach(event => {
let methodName = event[0];
event.shift();
let integrationOptions = event[0].message.integrations;
for (let i = 0; i < this.clientIntegrationObjects.length; i++) {
if (
integrationOptions[this.clientIntegrationObjects[i].name] ||
(integrationOptions[this.clientIntegrationObjects[i].name] ==
undefined &&
integrationOptions["All"])
) {
try {
this.clientIntegrationObjects[i][methodName](...event);
} catch (error) {
handleError(error);
replayEvents(object){
if(object.successfullyLoadedIntegration.length + object.failedToBeLoadedIntegration.length == object.clientIntegrations.length){
object.clientIntegrationObjects = object.successfullyLoadedIntegration;
//send the queued events to the fetched integration
object.toBeProcessedByIntegrationArray.forEach(event => {
let methodName = event[0];
event.shift();
let integrationOptions = event[0].message.integrations;
for (let i = 0; i < object.clientIntegrationObjects.length; i++) {
if (
integrationOptions[object.clientIntegrationObjects[i].name] ||
(integrationOptions[object.clientIntegrationObjects[i].name] ==
undefined &&
integrationOptions["All"])
) {
try {
object.clientIntegrationObjects[i][methodName](...event);
} catch (error) {
handleError(error);
}
}
}
});
object.toBeProcessedByIntegrationArray = [];
}
}

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


isInitialized(instance, time = 0){
return new Promise((resolve) => {
if(instance.isLoaded()){
this.successfullyLoadedIntegration.push(instance);
return resolve(this);
}
if(time >= MAX_WAIT_FOR_INTEGRATION_LOAD){
console.log("====max wait over====")
this.failedToBeLoadedIntegration.push(instance);
return resolve(this);
}

this.pause(INTEGRATION_LOAD_CHECK_INTERVAL).then(() => {
return this.isInitialized(instance, time + INTEGRATION_LOAD_CHECK_INTERVAL ).then(resolve);
});
});

this.toBeProcessedByIntegrationArray = [];

}

/**
Expand Down
110 changes: 74 additions & 36 deletions rudder-client-javascript/analytics/dist/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ var analytics = (function (exports) {

var CONFIG_URL = "https://api.rudderlabs.com/sourceConfig"; //"https://api.rudderlabs.com/workspaceConfig";
var FLUSH_INTERVAL_DEFAULT = 5000;
var MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
var INTEGRATION_LOAD_CHECK_INTERVAL = 1000;
/* module.exports = {
MessageType: MessageType,
ECommerceParamNames: ECommerceParamNames,
Expand Down Expand Up @@ -297,7 +299,7 @@ var analytics = (function (exports) {
value: function init() {
var hubspotJs = "http://js.hs-scripts.com/" + this.hubId + ".js";
ScriptLoader("hubspot-integration", hubspotJs);
console.log("===in init===");
console.log("===in init HS===");
}
}, {
key: "identify",
Expand Down Expand Up @@ -384,8 +386,8 @@ var analytics = (function (exports) {
_hsq.push(["trackPageView"]);
}
}, {
key: "loaded",
value: function loaded() {
key: "isLoaded",
value: function isLoaded() {
console.log("in hubspot isLoaded");
return !!(window._hsq && window._hsq.push !== Array.prototype.push);
}
Expand Down Expand Up @@ -467,10 +469,10 @@ var analytics = (function (exports) {
ga('send', 'pageview');
}
}, {
key: "loaded",
value: function loaded() {
key: "isLoaded",
value: function isLoaded() {
console.log("in GA isLoaded");
console.log("browser not implemented");
return !!window.gaplugins;
}
}]);

Expand Down Expand Up @@ -1147,7 +1149,7 @@ var analytics = (function (exports) {
/*#__PURE__*/
function () {
/**
*Creates an instance of Analytics.
* Creates an instance of Analytics.
* @memberof Analytics
*/
function Analytics() {
Expand All @@ -1158,6 +1160,8 @@ var analytics = (function (exports) {
this.clientIntegrations = [];
this.configArray = [];
this.clientIntegrationObjects = undefined;
this.successfullyLoadedIntegration = [];
this.failedToBeLoadedIntegration = [];
this.toBeProcessedArray = [];
this.toBeProcessedByIntegrationArray = [];
this.storage = new Storage$1();
Expand Down Expand Up @@ -1221,47 +1225,81 @@ var analytics = (function (exports) {
if (intg === "HS") {
var hubId = configArray[i].hubId;
var intgInstance = new intgClass(hubId);
intgInstance.init();
intgInstance.init(); //this.clientIntegrationObjects.push(intgInstance);

_this.clientIntegrationObjects.push(intgInstance);
_this.isInitialized(intgInstance).then(_this.replayEvents);
}

if (intg === "GA") {
var trackingID = configArray[i].trackingID;

var _intgInstance = new intgClass(trackingID);

_intgInstance.init();
_intgInstance.init(); //this.clientIntegrationObjects.push(intgInstance);

_this.clientIntegrationObjects.push(_intgInstance);

_this.isInitialized(_intgInstance).then(_this.replayEvents);
}
}); // Add GA forcibly for tests , TODO : Remove

/* let GAClass = integrations["GA"];
let GAInstance = new GAClass("UA-143161493-8");
GAInstance.init();
console.log("GA initialized");
this.clientIntegrationObjects.push(GAInstance); */
//send the queued events to the fetched integration

this.toBeProcessedByIntegrationArray.forEach(function (event) {
var methodName = event[0];
event.shift();
var integrationOptions = event[0].message.integrations;

for (var _i = 0; _i < _this.clientIntegrationObjects.length; _i++) {
if (integrationOptions[_this.clientIntegrationObjects[_i].name] || integrationOptions[_this.clientIntegrationObjects[_i].name] == undefined && integrationOptions["All"]) {
try {
var _this$clientIntegrati;

(_this$clientIntegrati = _this.clientIntegrationObjects[_i])[methodName].apply(_this$clientIntegrati, _toConsumableArray(event));
} catch (error) {
handleError(error);
});
}
}, {
key: "replayEvents",
value: function replayEvents(object) {
if (object.successfullyLoadedIntegration.length + object.failedToBeLoadedIntegration.length == object.clientIntegrations.length) {
object.clientIntegrationObjects = object.successfullyLoadedIntegration; //send the queued events to the fetched integration

object.toBeProcessedByIntegrationArray.forEach(function (event) {
var methodName = event[0];
event.shift();
var integrationOptions = event[0].message.integrations;

for (var i = 0; i < object.clientIntegrationObjects.length; i++) {
if (integrationOptions[object.clientIntegrationObjects[i].name] || integrationOptions[object.clientIntegrationObjects[i].name] == undefined && integrationOptions["All"]) {
try {
var _object$clientIntegra;

(_object$clientIntegra = object.clientIntegrationObjects[i])[methodName].apply(_object$clientIntegra, _toConsumableArray(event));
} catch (error) {
handleError(error);
}
}
}
});
object.toBeProcessedByIntegrationArray = [];
}
}
}, {
key: "pause",
value: function pause(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
}, {
key: "isInitialized",
value: function isInitialized(instance) {
var _this2 = this;

var time = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return new Promise(function (resolve) {
if (instance.isLoaded()) {
_this2.successfullyLoadedIntegration.push(instance);

return resolve(_this2);
}

if (time >= MAX_WAIT_FOR_INTEGRATION_LOAD) {
console.log("====max wait over====");

_this2.failedToBeLoadedIntegration.push(instance);

return resolve(_this2);
}

_this2.pause(INTEGRATION_LOAD_CHECK_INTERVAL).then(function () {
return _this2.isInitialized(instance, time + INTEGRATION_LOAD_CHECK_INTERVAL).then(resolve);
});
});
this.toBeProcessedByIntegrationArray = [];
}
/**
* Process page params and forward to page call
Expand Down Expand Up @@ -1594,8 +1632,8 @@ var analytics = (function (exports) {
instance.toBeProcessedArray.push(window.analytics[i]);
}

for (var _i2 = 0; _i2 < instance.toBeProcessedArray.length; _i2++) {
var event = _toConsumableArray(instance.toBeProcessedArray[_i2]);
for (var _i = 0; _i < instance.toBeProcessedArray.length; _i++) {
var event = _toConsumableArray(instance.toBeProcessedArray[_i]);

var _method = event[0];
event.shift();
Expand Down
2 changes: 1 addition & 1 deletion rudder-client-javascript/analytics/dist/browser.min.js

Large diffs are not rendered by default.

1,528 changes: 1,527 additions & 1 deletion rudder-client-javascript/analytics/dist/node.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions rudder-client-javascript/analytics/integrations/GA/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class GA {
ga('send', 'pageview');
}

loaded() {
isLoaded() {
console.log("in GA isLoaded");
console.log("browser not implemented");
return !!window.gaplugins
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HubSpot {
let hubspotJs = "http://js.hs-scripts.com/" + this.hubId + ".js";
ScriptLoader("hubspot-integration", hubspotJs);

console.log("===in init===");
console.log("===in init HS===");
}

identify(rudderElement) {
Expand Down Expand Up @@ -88,7 +88,7 @@ class HubSpot {
_hsq.push(["trackPageView"]);
}

loaded() {
isLoaded() {
console.log("in hubspot isLoaded");
return !!(window._hsq && window._hsq.push !== Array.prototype.push);
}
Expand Down
6 changes: 3 additions & 3 deletions rudder-client-javascript/analytics/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default {
input: "analytics.js",
external: ["Xmlhttprequest", "universal-analytics"],
output: [
{
file: "dist/browser.js",
/* {
file: "dist/browser.min.js",
format: "iife",
name: "analytics"
},
}, */
{
file: "dist/node.js",
format: "cjs"
Expand Down
7 changes: 6 additions & 1 deletion rudder-client-javascript/analytics/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ let FLUSH_QUEUE_SIZE = 30;

let FLUSH_INTERVAL_DEFAULT = 5000;

const MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
const INTEGRATION_LOAD_CHECK_INTERVAL = 1000;

export {
MessageType,
ECommerceParamNames,
Expand All @@ -84,7 +87,9 @@ export {
BASE_URL,
CONFIG_URL,
FLUSH_QUEUE_SIZE,
FLUSH_INTERVAL_DEFAULT
FLUSH_INTERVAL_DEFAULT,
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL
};
/* module.exports = {
MessageType: MessageType,
Expand Down

0 comments on commit 4712add

Please sign in to comment.