settings = $settings;
// Early in
: Consent Mode defaults must come before any other tag.
add_action('wp_head', [$this, 'verification'], 1);
add_action('wp_head', [$this, 'tag'], 2);
add_action('wp_head', [$this, 'metaPixel'], 3);
}
public function verification(): void
{
$code = (string) $this->settings->get('analytics.tracking.verification', '');
if ($code !== '' && (is_front_page() || is_home())) {
printf('' . "\n", esc_attr($code));
}
}
public function tag(): void
{
$id = (string) $this->settings->get('analytics.tracking.measurement_id', '');
if ($id === '' || is_preview() || is_customize_preview()) {
return;
}
$track = ! ($this->settings->get('analytics.tracking.exclude_editors', true) && current_user_can('edit_posts'));
/**
* Filters whether the GA4 tag is printed on this request.
*
* @param bool $track
* @param string $id Measurement ID.
*/
if (! apply_filters('featherdash/tracking/enabled', $track, $id)) {
return;
}
/**
* Filters the Consent Mode defaults sent before the tag loads.
* Return an empty array to send none.
*
* @param array $defaults
*/
$defaults = $this->settings->get('analytics.tracking.consent_mode', true)
? (array) apply_filters('featherdash/tracking/consent_defaults', [
'ad_storage' => 'denied',
'ad_user_data' => 'denied',
'ad_personalization' => 'denied',
'analytics_storage' => 'denied',
'wait_for_update' => 500,
])
: [];
$consent = $defaults !== [] ? "gtag('consent','default'," . wp_json_encode($defaults) . ');' : '';
/**
* Filters the parameters of gtag('config'), e.g. `['debug_mode' => true]`.
*
* @param array $config
* @param string $id
*/
$config = (array) apply_filters('featherdash/tracking/config', [], $id);
// Printed here rather than enqueued: Consent Mode defaults must come before other tags.
wp_print_script_tag([
'async' => true,
'src' => esc_url('https://www.googletagmanager.com/gtag/js?id=' . rawurlencode($id)),
]);
wp_print_inline_script_tag(
'window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}'
. $consent
. "gtag('js',new Date());gtag('config'," . wp_json_encode($id) . ($config !== [] ? ',' . wp_json_encode($config) : '') . ');'
);
}
/**
* The Meta Pixel base code. With "Wait for cookie consent" on, consent is
* revoked before init and granted once the visitor allows marketing
* cookies: through the WP Consent API (Complianz, CookieAdmin, CookieYes…)
* or Cookiebot's own callback. The