Skip to content

Commit

Permalink
review commits fixes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam4415 committed Dec 18, 2020
1 parent a036105 commit 720b4ac
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 129 deletions.
70 changes: 70 additions & 0 deletions __tests__/data/GA4_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const rudderanalytics = [];

rudderanalytics.track("Products Searched", {
query: "HDMI cable",
});

rudderanalytics.track("Product List Viewed", {
list_id: "list1",
category: "What's New",
products: [
{
product_id: "223344ffdds3ff3",
sku: "12345",
name: "Just Another Game",
price: 22,
position: 2,
category: "Games and Entertainment",
url: "https://www.myecommercewebsite.com/product",
image_url: "https://www.myecommercewebsite.com/product/path.jpg",
},
{
product_id: "343344ff5567ff3",
sku: "12346",
name: "Wrestling Trump Cards",
price: 4,
position: 21,
category: "Card Games",
},
],
});

rudderanalytics.track("Product List Filtered", {
list_id: "dealoftheday",
filters: [
{
type: "department",
value: "health",
},
{
type: "price",
value: "under-$75",
},
],
sorts: [
{
type: "price",
value: "asc",
},
],
products: [
{
product_id: "5034221345ffcd672315011",
sku: "12345",
name: "Whey Protein",
price: 55.45,
position: 1,
category: "health",
url: "https://www.myecommercewebsite.com/product/product1123",
image_url: "https://www.example.com/product/1123.jpg",
},
{
product_id: "121244455323232326677232",
sku: "345667",
name: "Boost",
price: 47.85,
position: 12,
category: "health",
},
],
});
28 changes: 28 additions & 0 deletions __tests__/data/GA4_output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const gtag = [];

gtag("event", "search", { search_term: "HDMI cable" });

gtag("event", "view_item_list", {
item_list_id: "list1",
items: [
{
item_id: "223344ffdds3ff3",
item_name: "Just Another Game",
price: 22,
index: 2,
item_category: "Games and Entertainment",
item_list_id: "list1",
item_list_name: "What's New",
},
{
item_id: "343344ff5567ff3",
item_name: "Wrestling Trump Cards",
price: 4,
index: 21,
item_category: "Card Games",
item_list_id: "list1",
item_list_name: "What's New",
},
],
item_list_name: "What's New",
});
1 change: 0 additions & 1 deletion integrations/GA4/ECommerceEventConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const eventParametersConfigArray = [
{ src: "affiliation", dest: ["affiliation"] },
{ src: "shipping", dest: ["shipping"] },
{ src: "tax", dest: ["tax"] },
{ src: "affiliation", dest: ["affiliation"] },
{ src: "total", dest: ["value"] },
{ src: "checkout_id", dest: ["transaction_id"] },
];
Expand Down
32 changes: 9 additions & 23 deletions integrations/GA4/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ export default class GA4 {
}
/* utility functions --- Ends here --- */

getDestinationItemProperties(products, item) {
const items = [];
let obj = {};
products.forEach((p) => {
obj = {
...getDestinationEventProperties(p, itemParametersConfigArray),
...(item && item[0]),
};
items.push(obj);
});
return items;
}

track(rudderElement) {
let { event } = rudderElement.message;
const { properties } = rudderElement.message;
Expand All @@ -93,10 +80,10 @@ export default class GA4 {
throw Error("Cannot call un-named/reserved named track event");
}

const obj = getDestinationEventName(event);
if (obj) {
const eventMappingObj = getDestinationEventName(event);
if (eventMappingObj) {
if (products && Array.isArray(products)) {
event = obj.dest;
event = eventMappingObj.dest;
// eslint-disable-next-line no-const-assign
destinationProperties = getDestinationEventProperties(
properties,
Expand All @@ -107,8 +94,8 @@ export default class GA4 {
destinationProperties.items
);
} else {
event = obj.dest;
if (!obj.hasItem) {
event = eventMappingObj.dest;
if (!eventMappingObj.hasItem) {
// eslint-disable-next-line no-const-assign
destinationProperties = getDestinationEventProperties(
properties,
Expand Down Expand Up @@ -139,13 +126,12 @@ export default class GA4 {
}

page(rudderElement) {
const page =
rudderElement.message.context && rudderElement.message.context.page;
if (!page) return;
const pageProps = rudderElement.message.properties;
if (!pageProps) return;
if (this.extendPageViewParams) {
window.gtag("event", "page_view", page);
window.gtag("event", "page_view", pageProps);
} else {
window.gtag("event", "page_view", getPageViewProperty(page));
window.gtag("event", "page_view", getPageViewProperty(pageProps));
}
}
}
209 changes: 114 additions & 95 deletions integrations/GA4/utility.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,118 @@
import {
eventNamesConfigArray,
itemParametersConfigArray,
} from "./ECommerceEventConfig";

import { pageEventParametersConfigArray } from "./PageEventConfig";

function isReservedName(name) {
const reservedEventNames = [
"ad_activeview",
"ad_click",
"ad_exposure",
"ad_impression",
"ad_query",
"adunit_exposure",
"app_clear_data",
"app_install",
"app_update",
"app_remove",
"error",
"first_open",
"first_visit",
"in_app_purchase",
"notification_dismiss",
"notification_foreground",
"notification_open",
"notification_receive",
"os_update",
"screen_view",
"session_start",
"user_engagement",
];

return reservedEventNames.includes(name);
}

function getDestinationEventName(events) {
return eventNamesConfigArray.find((p) =>
p.src.includes(events.toLowerCase())
);
}

function getDestinationEventProperties(props, destParameter) {
const destinationProperties = {};
const item = {};
Object.keys(props).forEach((key) => {
destParameter.forEach((param) => {
if (key === param.src) {
if (Array.isArray(param.dest)) {
param.dest.forEach((d) => {
const result = d.split(".");
// Here we only support mapping single level object mapping.
// To Do Future Scope :: implement using recursion to handle multi level prop mapping
if (result.length > 1) {
const levelOne = result[0];
const levelTwo = result[1];
item[levelTwo] = props[key];
if (!destinationProperties[levelOne]) {
destinationProperties[levelOne] = [];
destinationProperties[levelOne].push(item);
}
} else {
destinationProperties[result] = props[key];
eventNamesConfigArray,
itemParametersConfigArray,
} from "./ECommerceEventConfig";

import { pageEventParametersConfigArray } from "./PageEventConfig";

function isReservedName(name) {
const reservedEventNames = [
"ad_activeview",
"ad_click",
"ad_exposure",
"ad_impression",
"ad_query",
"adunit_exposure",
"app_clear_data",
"app_install",
"app_update",
"app_remove",
"error",
"first_open",
"first_visit",
"in_app_purchase",
"notification_dismiss",
"notification_foreground",
"notification_open",
"notification_receive",
"os_update",
"screen_view",
"session_start",
"user_engagement",
];

return reservedEventNames.includes(name);
}

function getDestinationEventName(event) {
return eventNamesConfigArray.find((p) =>
p.src.includes(event.toLowerCase())
);
}

/*
props: {key: val, key2:val2}
destParameter: [{ src: "s", dest: ["d"] }, { src: "sr", dest: ["de", "ef"] }]
output: {
"item_list_id": "list1",
"items": [
{
"item_id": "223344ffdds3ff3",
"item_name": "Just Another Game",
},
{
"item_id": "343344ff5567ff3",
"item_name": "Wrestling Trump Cards",
"price": 4,
"index": 21,
}
],
"item_list_name": "What's New"
}
*/
function getDestinationEventProperties(props, destParameterConfig) {
const destinationProperties = {};
const item = {};
Object.keys(props).forEach((key) => {
destParameterConfig.forEach((param) => {
if (key === param.src) {
if (Array.isArray(param.dest)) {
param.dest.forEach((d) => {
const result = d.split(".");
// Here we only support mapping single level object mapping.
// To Do Future Scope :: implement using recursion to handle multi level prop mapping
if (result.length > 1) {
const levelOne = result[0];
const levelTwo = result[1];
item[levelTwo] = props[key];
if (!destinationProperties[levelOne]) {
destinationProperties[levelOne] = [];
destinationProperties[levelOne].push(item);
}
});
} else {
destinationProperties[param.dest] = props[key];
}
} else {
destinationProperties[result] = props[key];
}
});
} else {
destinationProperties[param.dest] = props[key];
}
});
}
});
return destinationProperties;
}
function getDestinationItemProperties(products, item) {
const items = [];
let obj = {};
products.forEach((p) => {
obj = {
...getDestinationEventProperties(p, itemParametersConfigArray),
...(item && item[0]),
};
items.push(obj);
});
return items;
}
function getPageViewProperty(props) {
return getDestinationEventProperties(props, pageEventParametersConfigArray);
}
export {
isReservedName,
getDestinationEventName,
getDestinationEventProperties,
getDestinationItemProperties,
getPageViewProperty,
};
});
return destinationProperties;
}

function getDestinationItemProperties(products, item) {
const items = [];
let obj = {};
products.forEach((p) => {
obj = {
...getDestinationEventProperties(p, itemParametersConfigArray),
...(item && item[0]),
};
items.push(obj);
});
return items;
}

function getPageViewProperty(props) {
return getDestinationEventProperties(props, pageEventParametersConfigArray);
}

export {
isReservedName,
getDestinationEventName,
getDestinationEventProperties,
getDestinationItemProperties,
getPageViewProperty,
};
Loading

0 comments on commit 720b4ac

Please sign in to comment.