File size: 5,192 Bytes
1e92f2d |
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 152 153 154 155 156 157 158 159 160 161 162 163 |
import { getCurrentUser } from '@automattic/calypso-analytics';
import debug from 'debug';
import { GA4 } from 'calypso/lib/analytics/ad-tracking';
import isAkismetCheckout from '../akismet/is-akismet-checkout';
import isJetpackCheckout from '../jetpack/is-jetpack-checkout';
import isJetpackCloud from '../jetpack/is-jetpack-cloud';
import { mayWeTrackByTracker } from './tracker-buckets';
const gaDebug = debug( 'calypso:analytics:ga' );
let initialized = false;
function initialize() {
if ( ! initialized ) {
const params = {
send_page_view: false,
...getGoogleAnalyticsDefaultConfig(),
};
// We enable custom cross-domain linking for Akismet and Jetpack checkouts + Jetpack Cloud
if ( isAkismetCheckout() || isJetpackCloud() || isJetpackCheckout() ) {
const queryParams = new URLSearchParams( location.search );
const gl = queryParams.get( '_gl' );
// If we have a _gl query param, cross-domain linking is done automatically
if ( ! gl ) {
// Setting cross-domain manually: https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup
params.client_id = queryParams.get( '_gl_cid' );
params.session_id = queryParams.get( '_gl_sid' );
}
}
gaDebug( 'parameters:', params );
GA4.setup( params );
initialized = true;
}
}
export const gaRecordPageView = makeGoogleAnalyticsTrackingFunction( function recordPageView(
urlPath,
pageTitle,
useJetpackGoogleAnalytics = false,
useAkismetGoogleAnalytics = false,
useA8CForAgenciesGoogleAnalytics = false
) {
gaDebug(
'Recording Page View ~ [URL: ' +
urlPath +
'] [Title: ' +
pageTitle +
'] [useJetpackGoogleAnalytics: ' +
useJetpackGoogleAnalytics +
'] [useAksiemtGoogleAnalytics: ' +
useAkismetGoogleAnalytics +
'] [useA8CForAgenciesGoogleAnalytics: ' +
useA8CForAgenciesGoogleAnalytics +
']'
);
const getGa4PropertyGtag = () => {
if ( useJetpackGoogleAnalytics ) {
return GA4.Ga4PropertyGtag.JETPACK;
}
if ( useAkismetGoogleAnalytics ) {
return GA4.Ga4PropertyGtag.AKISMET;
}
if ( useA8CForAgenciesGoogleAnalytics ) {
return GA4.Ga4PropertyGtag.A8C_FOR_AGENCIES;
}
return GA4.Ga4PropertyGtag.WPCOM;
};
const ga4PropertyGtag = getGa4PropertyGtag();
GA4.firePageView( pageTitle, urlPath, ga4PropertyGtag );
} );
/**
* Fires a generic Google Analytics event
*
* {string} category Is the string that will appear as the event category.
* {string} action Is the string that will appear as the event action in Google Analytics Event reports.
* {string} label Is the string that will appear as the event label.
* {string} value Is a non-negative integer that will appear as the event value.
*/
export const gaRecordEvent = makeGoogleAnalyticsTrackingFunction(
function recordEvent( category, action, label, value ) {
if ( 'undefined' !== typeof value && ! isNaN( Number( String( value ) ) ) ) {
value = Math.round( Number( String( value ) ) ); // GA requires an integer value.
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventValue
}
let debugText = 'Recording Event ~ [Category: ' + category + '] [Action: ' + action + ']';
if ( 'undefined' !== typeof label ) {
debugText += ' [Option Label: ' + label + ']';
}
if ( 'undefined' !== typeof value ) {
debugText += ' [Option Value: ' + value + ']';
}
gaDebug( debugText );
fireGoogleAnalyticsEvent( category, action, label, value );
}
);
/**
* Wrap Google Analytics with debugging, possible analytics supression, and initialization
*
* This method will display debug output if Google Analytics is suppresed, otherwise it will
* initialize and call the Google Analytics function it is passed.
* @see mayWeTrackByTracker
* @param {Function} func Google Analytics tracking function
* @returns {Function} Wrapped function
*/
export function makeGoogleAnalyticsTrackingFunction( func ) {
return function ( ...args ) {
if ( ! mayWeTrackByTracker( 'ga' ) ) {
gaDebug( '[Disallowed] analytics %s( %o )', func.name, args );
return;
}
initialize();
func( ...args );
};
}
/**
* Returns the default configuration for Google Analytics
* @returns {Object} GA's default config
*/
function getGoogleAnalyticsDefaultConfig() {
const currentUser = getCurrentUser();
return {
...( currentUser && { user_id: currentUser.hashedPii.ID } ),
anonymize_ip: true,
transport_type: 'function' === typeof window.navigator.sendBeacon ? 'beacon' : 'xhr',
use_amp_client_id: true,
custom_map: {
dimension3: 'client_id',
},
linker: { accept_incoming: true },
};
}
/**
* Fires a generic Google Analytics event
* @param {string} category Is the string that will appear as the event category.
* @param {string} action Is the string that will appear as the event action in Google Analytics Event reports.
* @param {string} label Is the string that will appear as the event label.
* @param {number} value Is a non-negative integer that will appear as the event value.
*/
function fireGoogleAnalyticsEvent( category, action, label, value ) {
window.gtag( 'event', action, {
event_category: category,
event_label: label,
value: value,
} );
}
|