Skip to content

Commit

Permalink
modified beacon logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumita Mandal committed Dec 22, 2021
1 parent 69140d2 commit 0651b47
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 57 deletions.
25 changes: 2 additions & 23 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
CONFIG_URL,
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL,
BEACON_PLUGIN_URL,
} from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
Expand Down Expand Up @@ -99,7 +98,6 @@ class Analytics {
};
this.loaded = false;
this.loadIntegration = true;
this.beaconPlugin = undefined;
}

/**
Expand Down Expand Up @@ -1026,27 +1024,8 @@ class Analytics {
}

if (options && options.useBeacon === true) {
const pluginName = "beaconQueue";
ScriptLoader(pluginName, BEACON_PLUGIN_URL);
const interval = setInterval(
function () {
if (window.hasOwnProperty(pluginName)) {
const intMod = window[pluginName];
clearInterval(interval);
try {
this.beaconPlugin = new intMod();
this.eventRepository.useBeacon = options.useBeacon;
} catch (e) {
logger.error(pluginName, " initialization failed", e);
}
}
}.bind(this),
100
);

setTimeout(() => {
clearInterval(interval);
}, MAX_WAIT_FOR_INTEGRATION_LOAD);
this.eventRepository.useBeacon = options.useBeacon;
this.eventRepository.initializeTransportMechanism();
}

function errorHandler(error) {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
"bundle-size-visual": "NODE_ENV=true ENV=prod PROD_DEBUG=true uglify=true visualizer=true rollup -c",
"size-build": "npm run buildProdBrowser",
"size": "npm run size-build && size-limit",
"changelog": "auto-changelog -p -t keepachangelog -u true -l false --sort-commits date-desc ",
"buildDevBrowserBeacon": "NODE_ENV=false uglify=false TRANSPORT=beacon rollup -c",
"buildProdBrowserBeacon": "NODE_ENV=false uglify=true TRANSPORT=beacon rollup -c"
"changelog": "auto-changelog -p -t keepachangelog -u true -l false --sort-commits date-desc "
},
"pre-commit": [
"prodTest",
Expand Down
23 changes: 3 additions & 20 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import * as npmPackage from "./dist/rudder-sdk-js/package.json";
let distFileName = "";
let { version } = webPackage;
let moduleType = "web";
let inputFileName = "analytics.js";

if (process.env.TRANSPORT == "beacon") {
inputFileName = "./utils/beaconQueue.js";
}
switch (process.env.ENV) {
case "prod":
switch (process.env.ENC) {
Expand Down Expand Up @@ -48,13 +43,7 @@ switch (process.env.ENV) {
}
break;
default:
switch (process.env.TRANSPORT) {
case "beacon":
distFileName = "dist/plugin/beaconQueue.js";
break;
default:
distFileName = "dist/browser.js";
}
distFileName = "dist/browser.js";
break;
}

Expand All @@ -68,16 +57,10 @@ if (process.env.NPM == "true") {
version = npmPackage.version;
moduleType = "npm";
} else {
let iifeFileName;
if (process.env.TRANSPORT == "beacon") {
iifeFileName = "beaconQueue";
} else {
iifeFileName = "rudderanalytics";
}
outputFiles.push({
file: distFileName,
format: "iife",
name: iifeFileName,
name: "rudderanalytics",
sourcemap:
process.env.PROD_DEBUG_INLINE == "true"
? "inline"
Expand All @@ -86,7 +69,7 @@ if (process.env.NPM == "true") {
}

export default {
input: inputFileName,
input: "analytics.js",
external: ["Xmlhttprequest", "universal-analytics"],
output: outputFiles,
plugins: [
Expand Down
19 changes: 17 additions & 2 deletions utils/EventRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getCurrentTimeFormatted, handleError, replacer } from "./utils";

import { RudderPayload } from "./RudderPayload";
import logger from "./logUtil";
import beaconQueue from "./storage/beaconQueue";
// import * as XMLHttpRequestNode from "Xmlhttprequest";

let XMLHttpRequestNode;
Expand Down Expand Up @@ -48,6 +49,7 @@ class EventRepository {
this.state = "READY";
this.batchSize = 0;
this.useBeacon = false;
this.beaconQueue = beaconQueue;

// previous implementation
// setInterval(this.preaparePayloadAndFlush, FLUSH_INTERVAL_DEFAULT, this);
Expand Down Expand Up @@ -206,7 +208,7 @@ class EventRepository {
* @param {RudderElement} rudderElement
* @memberof EventRepository
*/
enqueue(rudderElement, type, beaconQueue) {
enqueue(rudderElement, type) {
const message = rudderElement.getElementContent();

const headers = {
Expand All @@ -228,7 +230,7 @@ class EventRepository {

// modify the url for event specific endpoints
const url = this.url.slice(-1) == "/" ? this.url.slice(0, -1) : this.url;
if (this.useBeacon && beaconQueue != undefined) {
if (this.useBeacon) {
const targetUrl = `${url}/beacon/v1/batch`;
beaconQueue.enqueue(targetUrl, headers, message, this.writeKey);
} else {
Expand All @@ -240,6 +242,19 @@ class EventRepository {
});
}
}

sendQueueDataForBeacon() {
this.beaconQueue.sendDataFromQueueAndDestroyQueue();
}

initializeTransportMechanism() {
const sendQueueData = this.sendQueueDataForBeacon.bind(this);
const url = this.url.slice(-1) == "/" ? this.url.slice(0, -1) : this.url;
const targetUrl = `${url}/beacon/v1/batch`;
this.beaconQueue.url = targetUrl;
this.beaconQueue.writekey = this.writeKey;
window.addEventListener("unload", sendQueueData);
}
}
let eventRepository = new EventRepository();
export { eventRepository as EventRepository };
3 changes: 0 additions & 3 deletions utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ const BASE_URL = "https://hosted.rudderlabs.com"; // default to RudderStack
const CONFIG_URL =
"https://api.rudderlabs.com/sourceConfig/?p=process.module_type&v=process.package_version";

const BEACON_PLUGIN_URL = "https://cdn.rudderlabs.com/plugins/beaconQueue.js";

const FLUSH_QUEUE_SIZE = 30;

const FLUSH_INTERVAL_DEFAULT = 5000;
Expand All @@ -104,7 +102,6 @@ export {
FLUSH_INTERVAL_DEFAULT,
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL,
BEACON_PLUGIN_URL,
};
/* module.exports = {
MessageType: MessageType,
Expand Down
10 changes: 4 additions & 6 deletions utils/beaconQueue.js → utils/storage/beaconQueue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable class-methods-use-this */
import logger from "./logUtil";
import { Store } from "./storage/store";
import logger from "../logUtil";
import { Store } from "./store";

const defaults = {
queue: "queue",
Expand Down Expand Up @@ -42,8 +42,6 @@ class BeaconQueue {
}

enqueue(url, headers, message, writekey) {
this.url = url;
this.writekey = writekey;
let queue = this.getQueue() || [];
queue = queue.slice(-(this.maxItems - 1));
queue.push(message);
Expand Down Expand Up @@ -111,5 +109,5 @@ class BeaconQueue {
}
}
}

export default BeaconQueue;
const beaconQueue = new BeaconQueue();
export default beaconQueue;

0 comments on commit 0651b47

Please sign in to comment.