| import { getTracksAnonymousUserId, getCurrentUser } from '@automattic/calypso-analytics'; |
| import cookie from 'cookie'; |
| import { mayWeTrackByTracker } from '../tracker-buckets'; |
| import { |
| debug, |
| DCM_FLOODLIGHT_SESSION_COOKIE_NAME, |
| DCM_FLOODLIGHT_SESSION_LENGTH_IN_SECONDS, |
| } from './constants'; |
|
|
| |
| import './setup'; |
|
|
| |
| |
| |
| |
| export function recordParamsInFloodlightGtag( params ) { |
| if ( ! mayWeTrackByTracker( 'floodlight' ) ) { |
| return; |
| } |
|
|
| |
| const defaults = { |
| ...floodlightUserParams(), |
| |
| allow_custom_scripts: true, |
| }; |
|
|
| const finalParams = [ 'event', 'conversion', { ...defaults, ...params } ]; |
|
|
| debug( 'recordParamsInFloodlightGtag:', finalParams ); |
|
|
| window.gtag( ...finalParams ); |
| } |
|
|
| |
| |
| |
| |
| function floodlightUserParams() { |
| const params = {}; |
| const currentUser = getCurrentUser(); |
| const anonymousUserId = getTracksAnonymousUserId(); |
|
|
| if ( currentUser ) { |
| params.u4 = currentUser.hashedPii.ID; |
| } |
|
|
| if ( anonymousUserId ) { |
| params.u5 = anonymousUserId; |
| } |
|
|
| return params; |
| } |
|
|
| |
| |
| |
| |
| function floodlightSessionId() { |
| const cookies = cookie.parse( document.cookie ); |
|
|
| const existingSessionId = cookies[ DCM_FLOODLIGHT_SESSION_COOKIE_NAME ]; |
| if ( existingSessionId ) { |
| debug( 'Floodlight: Existing session: ' + existingSessionId ); |
| return existingSessionId; |
| } |
|
|
| |
| const newSessionId = crypto.randomUUID().replace( new RegExp( '-', 'g' ), '' ); |
| debug( 'Floodlight: New session: ' + newSessionId ); |
| return newSessionId; |
| } |
|
|
| |
| |
| |
| |
| |
| export function recordPageViewInFloodlight( urlPath ) { |
| if ( ! mayWeTrackByTracker( 'floodlight' ) ) { |
| return; |
| } |
|
|
| const sessionId = floodlightSessionId(); |
|
|
| |
| document.cookie = cookie.serialize( DCM_FLOODLIGHT_SESSION_COOKIE_NAME, sessionId, { |
| maxAge: DCM_FLOODLIGHT_SESSION_LENGTH_IN_SECONDS, |
| } ); |
|
|
| debug( 'retarget: recordPageViewInFloodlight: wpvisit' ); |
| recordParamsInFloodlightGtag( { |
| session_id: sessionId, |
| u6: urlPath, |
| u7: sessionId, |
| send_to: 'DC-6355556/wordp0/wpvisit+per_session', |
| } ); |
|
|
| debug( 'retarget: recordPageViewInFloodlight: wppv' ); |
| recordParamsInFloodlightGtag( { |
| u6: urlPath, |
| u7: sessionId, |
| send_to: 'DC-6355556/wordp0/wppv+standard', |
| } ); |
| } |
|
|