-
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.
- Loading branch information
1 parent
1e8b6bb
commit 09a7f2a
Showing
2 changed files
with
170 additions
and
170 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 |
---|---|---|
@@ -1,220 +1,220 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import md5 from "md5"; | ||
import logger from "../../utils/logUtil"; | ||
import ScriptLoader from "../ScriptLoader"; | ||
import md5 from "md5"; | ||
|
||
class Criteo{ | ||
constructor(config) { | ||
this.name = "Criteo", | ||
this.hash_method = config.hashMethod, | ||
this.accountId = config.accountId, | ||
this.url = config.homePageUrl, | ||
this.deviceType = /iPad/.test(navigator.userAgent) | ||
? "t" | ||
: /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test( | ||
navigator.userAgent | ||
) | ||
? "m" | ||
: "d"; | ||
this.fieldMapping = config.fieldMapping; | ||
} | ||
|
||
class Criteo { | ||
constructor(config) { | ||
this.name = "Criteo"; | ||
this.hash_method = config.hashMethod; | ||
this.accountId = config.accountId; | ||
this.url = config.homePageUrl; | ||
// eslint-disable-next-line no-nested-ternary | ||
this.deviceType = /iPad/.test(navigator.userAgent) | ||
? "t" | ||
: /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test( | ||
navigator.userAgent | ||
) | ||
? "m" | ||
: "d"; | ||
this.fieldMapping = config.fieldMapping; | ||
} | ||
|
||
init() { | ||
init() { | ||
logger.debug("===in init Criteo==="); | ||
if (!this.accountId) { | ||
return; | ||
return; | ||
} | ||
window.criteo_q = window.criteo_q || []; | ||
|
||
ScriptLoader( | ||
"Criteo", | ||
`//dynamic.criteo.com/js/ld/ld.js?a=${this.accountId}` | ||
); | ||
window.criteo_q.push({ event: 'setAccount', account: account }); | ||
window.criteo_q.push({ event: 'setSiteType', type: this.deviceType }); | ||
window.criteo_q.push({ event: "setAccount", account: this.accountId }); | ||
window.criteo_q.push({ event: "setSiteType", type: this.deviceType }); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
isLoaded() { | ||
logger.debug("in Criteo isLoaded"); | ||
return !!(window.criteo_q && window.criteo_q.push !== Array.prototype.push); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
isReady() { | ||
logger.debug("in Criteo isReady"); | ||
return !!(window.criteo_q && window.criteo_q.push !== Array.prototype.push); | ||
} | ||
|
||
handleCommonFields (rudderElement) { | ||
const {message} = rudderElement; | ||
const {properties} = message; | ||
handleCommonFields(rudderElement) { | ||
const { message } = rudderElement; | ||
const { properties } = message; | ||
|
||
let setEmail = {}; | ||
let setZipcode = {}; | ||
const setEmail = {}; | ||
const setZipcode = {}; | ||
|
||
let finalRequest=[ | ||
{ event: "setCustomerId", id: md5(message.userId) }, | ||
{ event: "setRetailerVisitorId", id: md5(message.anonymousId)}, | ||
]; | ||
const finalRequest = [ | ||
{ event: "setCustomerId", id: md5(message.userId) }, | ||
{ event: "setRetailerVisitorId", id: md5(message.anonymousId) }, | ||
]; | ||
|
||
if(properties.email) { | ||
setEmail.event = "setEmail", | ||
setEmail.hash_method = this.hashMethod, | ||
setEmail.email = this.hashMethod === "md5" ? md5(properties.email) : properties.email; | ||
finalRequest.push(setEmail); | ||
if (properties.email) { | ||
setEmail.event = "setEmail"; | ||
setEmail.hash_method = this.hashMethod; | ||
setEmail.email = | ||
this.hashMethod === "md5" ? md5(properties.email) : properties.email; | ||
finalRequest.push(setEmail); | ||
} | ||
|
||
if(properties.zipCode) { | ||
setZipcode.event = "setZipcode", | ||
setZipcode.zipCode = properties.zipCode; | ||
finalRequest.push(setZipcode); | ||
if (properties.zipCode) { | ||
setZipcode.event = "setZipcode"; | ||
setZipcode.zipCode = properties.zipCode || properties.zip; | ||
finalRequest.push(setZipcode); | ||
} | ||
|
||
return finalRequest; | ||
} | ||
|
||
extraData (rudderElement) { | ||
const {message} = rudderElement; | ||
const extraData = {}; | ||
const fieldMapHashmap = getHashFromArray(this.fieldMapping); | ||
for (var field in fieldMapHashmap) { | ||
if (fieldMapHashmap.hasOwnProperty(field)) { | ||
if(message.properties[field]) { | ||
extraData[fieldMapHashmap[field]] = message.properties[field]; | ||
} | ||
} | ||
} | ||
return extraData; | ||
} | ||
// extraData (rudderElement) { | ||
// const {message} = rudderElement; | ||
// const extraData = {}; | ||
// const fieldMapHashmap = getHashFromArray(this.fieldMapping); | ||
// for (var field in fieldMapHashmap) { | ||
// if (fieldMapHashmap.hasOwnProperty(field)) { | ||
// if(message.properties[field]) { | ||
// extraData[fieldMapHashmap[field]] = message.properties[field]; | ||
// } | ||
// } | ||
// } | ||
// return extraData; | ||
// } | ||
|
||
page(rudderElement) { | ||
|
||
const { event } = rudderElement.message; | ||
|
||
let finalPayload = handleCommonFields(rudderElement); | ||
|
||
if(event === "home" || (this.url === window.location.href)) { | ||
const finalPayload = this.handleCommonFields(rudderElement); | ||
|
||
let homeEvent = { | ||
"event":"viewHome" | ||
} | ||
finalPayload.push(homeEvent); | ||
} | ||
const extraDataObject = extraData(rudderElement) | ||
if (Object.keys(extraDataObject).length !== 0) { | ||
finalPayload.push({event : 'setData',extraDataObject}); | ||
} | ||
if (event === "home" || (this.url && this.url === window.location.href)) { | ||
const homeEvent = { | ||
event: "viewHome", | ||
}; | ||
finalPayload.push(homeEvent); | ||
} | ||
// const extraDataObject = this.extraData(rudderElement) | ||
// if (Object.keys(extraDataObject).length !== 0) { | ||
// finalPayload.push({event : 'setData',extraDataObject}); | ||
// } | ||
|
||
window.criteo_q.push(finalPayload); | ||
|
||
} | ||
|
||
track(rudderElement) { | ||
const { event, properties } = rudderElement.message; | ||
|
||
let finalPayload = handleCommonFields(rudderElement); | ||
|
||
// Product tag | ||
if (event === "Product Viewed") { | ||
viewItemObject = { | ||
event: "viewItem", | ||
item: String(properties.product_id), | ||
price: properties.price, | ||
availability: properties.availability | ||
}; | ||
if (!viewItemObject.item) { | ||
// productId is madatory | ||
return; | ||
} | ||
finalPayload.push(viewItemObject); | ||
|
||
} | ||
|
||
// Basket/cart tag | ||
if (event === "Cart Viewed") { | ||
let productInfo = []; | ||
let elementaryProduct; | ||
properties.products.forEach((product) => { | ||
elementaryProduct = { | ||
id : String(product.product_id), | ||
price : product.price, | ||
quantity : product.quantity | ||
} | ||
|
||
if (productInfo.id) { | ||
// productId is madatory | ||
productInfo.push(elementaryProduct); | ||
} | ||
|
||
}); | ||
viewBasketObject = { | ||
event: "viewBasket", | ||
item: productInfo | ||
}; | ||
finalPayload.push(viewBasketObject); | ||
|
||
} | ||
// sales tag | ||
if(event === "Order Completed") { | ||
let productInfo = []; | ||
let elementaryProduct; | ||
properties.products.forEach((product) => { | ||
elementaryProduct = { | ||
id: String(product.product_id), | ||
price : product.price, | ||
quantity : product.quantity | ||
} | ||
if (elementaryProduct.id) { | ||
// productId is madatory | ||
productInfo.push(elementaryProduct); | ||
} | ||
}); | ||
trackTransactionObject = { | ||
event: "trackTransaction", | ||
id: String(properties.order_id), | ||
new_customer: properties.new_customer, | ||
deduplication: properties.deduplication, | ||
item: productInfo | ||
}; | ||
finalPayload.push(trackTransactionObject); | ||
|
||
const { event, properties } = rudderElement.message; | ||
|
||
const finalPayload = this.handleCommonFields(rudderElement); | ||
|
||
// Product tag | ||
if (event === "Product Viewed") { | ||
const viewItemObject = { | ||
event: "viewItem", | ||
item: String(properties.product_id), | ||
price: properties.price, | ||
availability: properties.availability, | ||
}; | ||
if (!viewItemObject.item) { | ||
// productId is madatory | ||
return; | ||
} | ||
finalPayload.push(viewItemObject); | ||
} | ||
|
||
// Category/keyword search/listing tag | ||
if (event === "Product List Viewed") { | ||
let productIdList = []; | ||
let filterObject = {}; | ||
let viewListObj = {}; | ||
properties.products.forEach((product)=>{ | ||
if(product.product_id) { | ||
productIdList.push(product.product_id) | ||
} | ||
}); | ||
|
||
if(properties.name && properties.value && (properties.operator && OPERATOR_LIST.includes(properties.operator))) { | ||
|
||
filterObject.name = properties.name; | ||
filterObject.value = properties.value; | ||
filterObject.operator = properties.operator; | ||
viewListObj.filters = [filterObject]; | ||
} | ||
|
||
viewListObj.event = "viewList"; | ||
viewListObj.item = productIdList; | ||
viewListObj.category = properties.category; | ||
viewListObj.keywords = properties.keywords; | ||
viewListObj.page_number = properties.page_number; | ||
|
||
finalPayload.push(viewListObj); | ||
// Basket/cart tag | ||
if (event === "Cart Viewed") { | ||
const productInfo = []; | ||
let elementaryProduct; | ||
properties.products.forEach((product) => { | ||
elementaryProduct = { | ||
id: String(product.product_id), | ||
price: product.price, | ||
quantity: product.quantity, | ||
}; | ||
|
||
if (productInfo.id) { | ||
// productId is madatory | ||
productInfo.push(elementaryProduct); | ||
} | ||
}); | ||
const viewBasketObject = { | ||
event: "viewBasket", | ||
item: productInfo, | ||
}; | ||
finalPayload.push(viewBasketObject); | ||
} | ||
// sales tag | ||
if (event === "Order Completed") { | ||
const productInfo = []; | ||
let elementaryProduct; | ||
properties.products.forEach((product) => { | ||
elementaryProduct = { | ||
id: String(product.product_id), | ||
price: product.price, | ||
quantity: product.quantity, | ||
}; | ||
if (elementaryProduct.id) { | ||
// productId is madatory | ||
productInfo.push(elementaryProduct); | ||
} | ||
}); | ||
const trackTransactionObject = { | ||
event: "trackTransaction", | ||
id: String(properties.order_id), | ||
new_customer: properties.new_customer, | ||
deduplication: properties.deduplication, | ||
item: productInfo, | ||
}; | ||
finalPayload.push(trackTransactionObject); | ||
} | ||
|
||
// Category/keyword search/listing tag | ||
if (event === "Product List Viewed") { | ||
const productIdList = []; | ||
const filterObject = {}; | ||
const viewListObj = {}; | ||
properties.products.forEach((product) => { | ||
if (product.product_id) { | ||
productIdList.push(product.product_id); | ||
} | ||
}); | ||
|
||
if ( | ||
properties.name && | ||
properties.value && | ||
properties.operator && | ||
OPERATOR_LIST.includes(properties.operator) | ||
) { | ||
filterObject.name = properties.name; | ||
filterObject.value = properties.value; | ||
filterObject.operator = properties.operator; | ||
viewListObj.filters = [filterObject]; | ||
} | ||
const extraDataObject = extraData(rudderElement) | ||
if (Object.keys(extraDataObject).length !== 0) { | ||
finalPayload.push({event : 'setData',extraData(rudderElement)}); | ||
} | ||
window.criteo_q.push(finalPayload); | ||
|
||
} | ||
|
||
viewListObj.event = "viewList"; | ||
viewListObj.item = productIdList; | ||
viewListObj.category = properties.category; | ||
viewListObj.keywords = properties.keywords; | ||
viewListObj.page_number = properties.page_number; | ||
|
||
finalPayload.push(viewListObj); | ||
} | ||
// const extraDataObject = this.extraData(rudderElement) | ||
// if (Object.keys(extraDataObject).length !== 0) { | ||
// let extraDataEvent = { | ||
// event : 'setData',extraD | ||
// } | ||
// finalPayload.push(extend(extraData, { event: 'setData' })); | ||
// } | ||
window.criteo_q.push(finalPayload); | ||
} | ||
} | ||
export default Criteo; | ||
export default Criteo; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { Criteo } from "./browser"; | ||
import Criteo from "./browser"; | ||
|
||
export default Criteo; |