All files index.js

91.43% Statements 32/35
72.73% Branches 8/11
84.62% Functions 11/13
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                      1x     1x   1x           16x                   1x 16x 16x                       1x 16x 16x   9x                         1x     16x         16x                                           1x 16x 16x 16x 16x         16x 16x 16x 16x 16x         16x                       1x                       1x   4x 4x               1x        
import '@adv-ui/boros-adevinta-plugin'
 
import './patchAnalytics'
import initTcfTracking from './tcf'
 
import {getAdobeVisitorData, getAdobeMCVisitorID} from './adobeRepository'
import {getPageReferrer, updatePageReferrer} from './referrer'
import {syncPixels} from './syncPixels'
import {getConfig} from './getConfig'
 
/* Initialize TCF Tracking with Segment */
initTcfTracking()
 
/* Default properties to be sent on all trackings */
const DEFAULT_PROPERTIES = {platform: 'web'}
/* ServerSide Forwarding values */
const SSF = {enabled: 0, disabled: 1}
 
/**
 * Get default properties using the constant and the window.__mpi object if available
 * @returns {{[key:string]: any}} Default properties to attach to track
 */
const getDefaultProperties = () => ({
  ...DEFAULT_PROPERTIES,
  ...getConfig('defaultProperties')
})
 
/**
 * Get user traits from global analytics object and put in the object
 * @param {object} traits Info of the user we already have
 * @returns {object} Traits with new merged info
 */
const mergeUserTraitsTo = traits => {
  const user = window.analytics.user()
  return {
    ...traits,
    anonymousId: user.anonymousId(),
    userId: user.id()
  }
}
 
/**
 * Get AdobeMarketingCloudVisitorId and merge into the integrations object
 * @param {object} integrations Object with all the integrations
 * @returns {Promise<object>} Integrations with the merged Adobe Analytics needed info
 */
const mergeAdobeAnalyticsIntegrationsTo = async (integrations = {}) => {
  const marketingCloudVisitorId = await getAdobeMCVisitorID()
  if (!marketingCloudVisitorId) return integrations
 
  return {
    ...integrations,
    'Adobe Analytics': {
      marketingCloudVisitorId
    }
  }
}
 
/**
 * Get data like traits and integrations to be added to the context object
 * @param {object} context Context object with all the actual info
 * @returns {Promise<object>} New context with all the previous info and the new one
 */
const decorateContextWithNeededData = async (context = {}) => {
  // TEMP: For now isDMPReady is always true as we're losing unexpected volumetry
  // on data analytics because of waiting user consents
  const [integrations, isDMPReady = true] = await Promise.all([
    mergeAdobeAnalyticsIntegrationsTo(context.integrations)
    // checkIsDMPReady()
  ])
 
  return {
    ...context,
    integrations,
    page: {
      ...context.page,
      referrer: getPageReferrer()
    },
    traits: {
      ssf: isDMPReady ? SSF.enabled : SSF.disabled,
      ...mergeUserTraitsTo(context.traits)
    }
  }
}
 
/**
 * The track method lets you record any actions your users perform.
 * @param {string} event The name of the event you’re tracking
 * @param {object} [properties] A dictionary of properties for the event.
 * @param {object} [context] A dictionary of options.
 * @param {function} [callback] A function executed after a short timeout, giving the browser time to make outbound requests first.
 * @returns {Promise}
 */
const track = (event, properties, context = {}, callback) =>
  new Promise(resolve => {
    window.analytics.ready(async function() {
      const newContext = await decorateContextWithNeededData(context)
      const newProperties = {
        ...getDefaultProperties(),
        ...properties
      }
 
      const newCallback = (...args) => {
        Iif (callback) callback(...args) // eslint-disable-line standard/no-callback-literal
        getAdobeMCVisitorID().then(marketingCloudVisitorId => {
          syncPixels(marketingCloudVisitorId).then(() => {
            resolve(...args)
          })
        })
      }
 
      window.analytics.track(event, newProperties, newContext, newCallback)
    })
  })
 
/**
 * Associate your users and their actions to a recognizable userId and traits.
 * @param {string} userId Id to identify the user.
 * @param {object} traits A dictionary of traits you know about the user, like their email or name.
 * @param {object} [options] A dictionary of options.
 * @param {function} [callback] A function executed after a short timeout, giving the browser time to make outbound requests first.
 * @returns {Promise}
 */
const identify = (userId, traits, options, callback) =>
  Promise.resolve(window.analytics.identify(userId, traits, options, callback))
 
/**
 * Record whenever a user sees a page of your website, along with any optional properties about the page.
 * It updates automatically the referrer from the context on SPA navigations.
 * @param {string} event The name of the event you’re tracking
 * @param {object} properties A dictionary of properties for the event.
 * @param {object} [context] A dictionary of options.
 * @param {function} [callback] A function executed after a short timeout, giving the browser time to make outbound requests first.
 * @returns {Promise}
 */
const page = (event, properties, context = {}, callback) => {
  // update the page referrer before tracking the event
  updatePageReferrer()
  return track(event, properties, context, callback)
}
 
/**
 * Resets the id, including anonymousId, and clear traits for the currently identified user and group.
 * NOTE: Only clears the cookies and localStorage set by analytics.
 * @returns {Promise}
 */
const reset = () => Promise.resolve(window.analytics.reset())
 
export {getAdobeVisitorData, getAdobeMCVisitorID, decorateContextWithNeededData}
export default {track, identify, page, reset}