-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from rudderlabs/query_string_parse_production
add query parse logic from url and send events ---> Production
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
||
/** | ||
|
@@ -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(); | ||
|
@@ -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]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters