-
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 #132 from rudderlabs/merge_prod
Merge prods ---> master
- Loading branch information
Showing
10 changed files
with
6,087 additions
and
3,078 deletions.
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
{ | ||
"loose": true | ||
} | ||
] | ||
], | ||
"ignore": ["node_modules/**",] | ||
], | ||
["@babel/plugin-transform-arrow-functions"] | ||
] | ||
} |
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
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 | ||
|
||
/** | ||
|
@@ -355,9 +361,13 @@ class Analytics { | |
(callback = properties), (options = properties = null); | ||
if (typeof name === "function") | ||
(callback = name), (options = properties = name = null); | ||
if (typeof category === "object") | ||
if ( | ||
typeof category === "object" && | ||
category != null && | ||
category != undefined | ||
) | ||
(options = name), (properties = category), (name = category = null); | ||
if (typeof name === "object") | ||
if (typeof name === "object" && name != null && name != undefined) | ||
(options = properties), (properties = name), (name = null); | ||
if (typeof category === "string" && typeof name !== "string") | ||
(name = category), (category = null); | ||
|
@@ -482,18 +492,18 @@ class Analytics { | |
*/ | ||
processPage(category, name, properties, options, callback) { | ||
const rudderElement = new RudderElementBuilder().setType("page").build(); | ||
if (name) { | ||
rudderElement.message.name = name; | ||
} | ||
if (!properties) { | ||
properties = {}; | ||
} | ||
if (name) { | ||
rudderElement.message.name = name; | ||
properties.name = name; | ||
} | ||
if (category) { | ||
rudderElement.message.category = category; | ||
properties.category = category; | ||
} | ||
if (properties) { | ||
rudderElement.message.properties = this.getPageProperties(properties); // properties; | ||
} | ||
rudderElement.message.properties = this.getPageProperties(properties); // properties; | ||
|
||
this.trackPage(rudderElement, options, callback); | ||
} | ||
|
@@ -630,7 +640,7 @@ class Analytics { | |
} | ||
|
||
// assign page properties to context | ||
rudderElement.message.context.page = getDefaultPageProperties(); | ||
// rudderElement.message.context.page = getDefaultPageProperties(); | ||
|
||
rudderElement.message.context.traits = { | ||
...this.userTraits, | ||
|
@@ -653,9 +663,7 @@ class Analytics { | |
} | ||
} | ||
|
||
if (options) { | ||
this.processOptionsParam(rudderElement, options); | ||
} | ||
this.processOptionsParam(rudderElement, options); | ||
logger.debug(JSON.stringify(rudderElement)); | ||
|
||
// structure user supplied integrations object to rudder format | ||
|
@@ -674,13 +682,17 @@ class Analytics { | |
); | ||
|
||
// try to first send to all integrations, if list populated from BE | ||
succesfulLoadedIntersectClientSuppliedIntegrations.forEach((obj) => { | ||
if (!obj.isFailed || !obj.isFailed()) { | ||
if (obj[type]) { | ||
obj[type](rudderElement); | ||
try { | ||
succesfulLoadedIntersectClientSuppliedIntegrations.forEach((obj) => { | ||
if (!obj.isFailed || !obj.isFailed()) { | ||
if (obj[type]) { | ||
obj[type](rudderElement); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} catch (err) { | ||
handleError({ message: `[sendToNative]:${err}` }); | ||
} | ||
|
||
// config plane native enabled destinations, still not completely loaded | ||
// in the page, add the events to a queue and process later | ||
|
@@ -713,6 +725,7 @@ class Analytics { | |
* @memberof Analytics | ||
*/ | ||
processOptionsParam(rudderElement, options) { | ||
const { type, properties } = rudderElement.message; | ||
const toplevelElements = [ | ||
"integrations", | ||
"anonymousId", | ||
|
@@ -733,18 +746,40 @@ class Analytics { | |
} | ||
} | ||
} | ||
// assign page properties to context.page | ||
rudderElement.message.context.page = | ||
type == "page" | ||
? this.getContextPageProperties(options, properties) | ||
: this.getContextPageProperties(options); | ||
} | ||
|
||
getPageProperties(properties) { | ||
getPageProperties(properties, options) { | ||
const defaultPageProperties = getDefaultPageProperties(); | ||
const optionPageProperties = options && options.page ? options.page : {}; | ||
for (const key in defaultPageProperties) { | ||
if (properties[key] === undefined) { | ||
properties[key] = defaultPageProperties[key]; | ||
properties[key] = | ||
optionPageProperties[key] || defaultPageProperties[key]; | ||
} | ||
} | ||
return properties; | ||
} | ||
|
||
// Assign page properties to context.page if the same property is not provided under context.page | ||
getContextPageProperties(options, properties) { | ||
const defaultPageProperties = getDefaultPageProperties(); | ||
const contextPageProperties = options && options.page ? options.page : {}; | ||
for (const key in defaultPageProperties) { | ||
if (contextPageProperties[key] === undefined) { | ||
contextPageProperties[key] = | ||
properties && properties[key] | ||
? properties[key] | ||
: defaultPageProperties[key]; | ||
} | ||
} | ||
return contextPageProperties; | ||
} | ||
|
||
/** | ||
* Clear user information | ||
* | ||
|
@@ -952,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(); | ||
|
@@ -996,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]); | ||
|
Oops, something went wrong.