|
|
import { getCurrentUser } from '@automattic/calypso-analytics'; |
|
|
import { clone, cloneDeep } from 'lodash'; |
|
|
import { mayWeTrackByTracker } from '../tracker-buckets'; |
|
|
import { debug, TRACKING_IDS } from './constants'; |
|
|
import { loadTrackingScripts } from './load-tracking-scripts'; |
|
|
|
|
|
|
|
|
import './setup'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function recordInCriteo( eventName, eventProps ) { |
|
|
if ( ! mayWeTrackByTracker( 'criteo' ) ) { |
|
|
debug( 'recordInCriteo: [Skipping] ad tracking is not allowed' ); |
|
|
return; |
|
|
} |
|
|
|
|
|
await loadTrackingScripts(); |
|
|
|
|
|
const events = []; |
|
|
const currentUser = getCurrentUser(); |
|
|
|
|
|
events.push( { event: 'setAccount', account: TRACKING_IDS.criteo } ); |
|
|
events.push( { event: 'setSiteType', type: criteoSiteType() } ); |
|
|
|
|
|
if ( currentUser ) { |
|
|
events.push( { event: 'setEmail', email: [ currentUser.hashedPii.email ] } ); |
|
|
} |
|
|
|
|
|
const conversionEvent = clone( eventProps ); |
|
|
conversionEvent.event = eventName; |
|
|
events.push( conversionEvent ); |
|
|
|
|
|
|
|
|
|
|
|
debug( 'recordInCriteo: ' + eventName, cloneDeep( events ) ); |
|
|
window.criteo_q.push( ...events ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function recordPlansViewInCriteo() { |
|
|
if ( ! mayWeTrackByTracker( 'criteo' ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const params = [ |
|
|
'viewItem', |
|
|
{ |
|
|
item: '1', |
|
|
}, |
|
|
]; |
|
|
debug( 'recordPlansViewInCriteo:', params ); |
|
|
recordInCriteo( ...params ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function recordViewCheckoutInCriteo( cart ) { |
|
|
if ( ! mayWeTrackByTracker( 'criteo' ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( cart.is_signup ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const params = [ |
|
|
'viewBasket', |
|
|
{ |
|
|
currency: cart.currency, |
|
|
item: cartToCriteoItems( cart ), |
|
|
}, |
|
|
]; |
|
|
debug( 'recordViewCheckoutInCriteo:', params ); |
|
|
recordInCriteo( ...params ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function cartToCriteoItems( cart ) { |
|
|
return cart.products.map( ( product ) => { |
|
|
return { |
|
|
id: product.product_id, |
|
|
price: product.cost, |
|
|
quantity: product.volume, |
|
|
}; |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function criteoSiteType() { |
|
|
if ( /iPad/.test( window.navigator.userAgent ) ) { |
|
|
return 't'; |
|
|
} |
|
|
|
|
|
if ( /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test( window.navigator.userAgent ) ) { |
|
|
return 'm'; |
|
|
} |
|
|
|
|
|
return 'd'; |
|
|
} |
|
|
|