Skip to content

Commit

Permalink
Merge pull request #128 from rudderlabs/query_string_parse_production
Browse files Browse the repository at this point in the history
add query parse logic from url and send events ---> Production
  • Loading branch information
sayan-mitra authored Sep 8, 2020
2 parents 10c68ae + fe16ef5 commit 5c63766
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
76 changes: 76 additions & 0 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/* eslint-disable no-param-reassign */
import Emitter from "component-emitter";
import after from "after";
import querystring from "component-querystring";
import {
getJSONTrimmed,
generateUUID,
Expand All @@ -35,6 +36,11 @@ import logger from "./utils/logUtil";
import { addDomEventHandlers } from "./utils/autotrack.js";
import ScriptLoader from "./integrations/ScriptLoader";

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

// https://unpkg.com/[email protected]/dist/browser.js

/**
Expand Down Expand Up @@ -981,6 +987,71 @@ class Analytics {
"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
);
}

/**
* parse the given query string into usable Rudder object
* @param {*} query
*/
parseQueryString(query) {
function getTraitsFromQueryObject(qObj) {
const traits = {};
Object.keys(qObj).forEach((key) => {
if (key.substr(0, queryDefaults.trait.length) == queryDefaults.trait) {
traits[key.substr(queryDefaults.trait.length)] = qObj[key];
}
});

return traits;
}

function getEventPropertiesFromQueryObject(qObj) {
const props = {};
Object.keys(qObj).forEach((key) => {
if (key.substr(0, queryDefaults.prop.length) == queryDefaults.prop) {
props[key.substr(queryDefaults.prop.length)] = qObj[key];
}
});

return props;
}

const returnObj = {};
const queryObject = querystring.parse(query);
const userTraits = getTraitsFromQueryObject(queryObject);
const eventProps = getEventPropertiesFromQueryObject(queryObject);
if (queryObject.ajs_uid) {
returnObj.userId = queryObject.ajs_uid;
returnObj.traits = userTraits;
}
if (queryObject.ajs_aid) {
returnObj.anonymousId = queryObject.ajs_aid;
}
if (queryObject.ajs_event) {
returnObj.event = queryObject.ajs_event;
returnObj.properties = eventProps;
}

return returnObj;
}
}

function pushDataToAnalyticsArray(argumentsArray, obj) {
if (obj.anonymousId) {
if (obj.userId) {
argumentsArray.unshift(
["setAnonymousId", obj.anonymousId],
["identify", obj.userId, obj.traits]
);
} else {
argumentsArray.unshift(["setAnonymousId", obj.anonymousId]);
}
} else if (obj.userId) {
argumentsArray.unshift(["identify", obj.userId, obj.traits]);
}

if (obj.event) {
argumentsArray.push(["track", obj.event, obj.properties]);
}
}

const instance = new Analytics();
Expand Down Expand Up @@ -1025,6 +1096,11 @@ if (
argumentsArray.shift();
}

// once loaded, parse querystring of the page url to send events
const parsedQueryObject = instance.parseQueryString(window.location.search);

pushDataToAnalyticsArray(argumentsArray, parsedQueryObject);

if (eventsPushedAlready && argumentsArray && argumentsArray.length > 0) {
for (let i = 0; i < argumentsArray.length; i++) {
instance.toBeProcessedArray.push(argumentsArray[i]);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rudder-analytics",
"version": "1.1.3",
"version": "1.1.4",
"description": "",
"main": "./dist/browser.min.js",
"size-limit": [
Expand Down Expand Up @@ -52,6 +52,7 @@
"component-cookie": "^1.1.4",
"component-each": "^0.2.6",
"component-emitter": "github:component/emitter",
"component-querystring": "^2.0.0",
"component-url": "^0.2.1",
"core-js": "^3.6.5",
"crypto-js": "3.1.9-1",
Expand Down

0 comments on commit 5c63766

Please sign in to comment.