All files syncPixels.js

87.18% Statements 34/39
77.78% Branches 14/18
91.67% Functions 11/12
91.43% Lines 32/35

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              1x 1x             1x 2x 2x 2x 2x 2x 2x   2x                           1x 1x   1x 1x 1x                 1x 1x   1x 1x 1x               1x 1x             1x   13x     6x 6x     1x   1x     1x 1x   5x     1x              
import {getConfig} from './getConfig'
 
/**
 * Create the URL of Demdex with a specified Marketing Cloud Visitor Id
 * @param {String} mcvid Marketing Cloud Visitor Id
 * @return {String} URL
 */
const createSyncPixelsUrl = mcvid =>
  `https://mpispain.demdex.net/event?d_mid=${mcvid}&d_orgid=05FF6243578784B37F000101%40AdobeOrg&d_rtbd=json&d_full=1&d_cts=2`
 
/**
 * Create a pixel by using a image src and log the info with the used ids
 * @param {{ ids: number[], pixelName: String, src: String}} params
 * @return {Promise}
 */
const createPixel = ({ids, pixelName, src}) => {
  const img = new window.Image()
  img.src = src
  return new Promise((resolve, reject) => {
    img.onload = () => {
      console.info(`Create segment in ${pixelName}`, ids)
      resolve()
    }
    img.onerror = () => {
      const err = `Error creating segments in ${pixelName}`
      console.error(err)
      reject(err)
    }
  })
}
 
/**
 * Sync with Facebook Ads Pixel
 * @param {string} pixelId
 * @param {number[]} ids
 * @return {Promise}
 */
const syncWithFacebook = (pixelId, ids) => {
  Iif (!pixelId) return Promise.resolve()
 
  const qs = ids.join(',')
  const src = `https://www.facebook.com/tr/?id=${pixelId}&ev=Adobe-Audience-Manager-Segment&cd[segID]=${qs}&noscript=1`
  return createPixel({ids, pixelName: 'Facebook Ads', src})
}
 
/**
 * Sync with Google Ads Pixel
 * @param {string} pixelId
 * @param {number[]} ids
 * @return {Promise}
 */
const syncWithGoogle = (pixelId, ids) => {
  Iif (!pixelId) return Promise.resolve()
 
  const qs = ids.join(';aam=')
  const src = `https://googleads.g.doubleclick.net/pagead/viewthroughconversion/${pixelId}/?guid=ON&script=0&data=aam=${qs}&value=0`
  return createPixel({ids, pixelName: 'Google Ads', src})
}
 
/**
 * Extract Ids from Demdex API response
 * @param {object} json
 * @return {String}
 */
const extractStringIds = json =>
  (json.stuff && json.stuff[0] && json.stuff[0].cv) || ''
 
/**
 * Retrieve ids from Demdex API and
 * @param {String} mcvid Marketing Cloud Visitor Id
 * @return {Promise}
 */
export const syncPixels = mcvid => {
  // check if we have a Marketing Cloud Visitor Id before continuing
  if (!mcvid) return Promise.resolve()
 
  // check if pixels ids are configured before continuing
  const {facebookPixelId, googleAdsPixelId} = getConfig('segmentWrapper') || {}
  if (!facebookPixelId && !googleAdsPixelId) return Promise.resolve()
 
  // if we have a MCVID and pixels are configured, proceed to sync pixels
  return window
    .fetch(createSyncPixelsUrl(mcvid))
    .then(res => res.json())
    .then(extractStringIds)
    .then(stringIds => {
      Eif (stringIds) {
        const ids = stringIds
          .split(/(\d+)/)
          .map(str => +str)
          .filter(Boolean)
 
        return Promise.all([
          syncWithGoogle(googleAdsPixelId, ids),
          syncWithFacebook(facebookPixelId, ids)
        ])
      }
    })
}