on every page.
* Uses priority 1 to load as early as possible.
*/
function acceptrics_inject_head_script() {
$account_id = get_option('acceptrics_account_id', '');
if (empty($account_id)) {
return;
}
// Account ID is sanitized on save (alphanumeric, hyphens, underscores only).
$safe_id = esc_attr($account_id);
echo '' . "\n";
}
add_action('wp_head', 'acceptrics_inject_head_script', 1);
/**
* Inject the Google tag snippet routing through the Acceptrics Relay when the
* relay is active. Runs at priority 2 (just after the consent banner at 1).
*
* For GA4 (G-XXXXXXXX): loads gtag.js from the relay subdomain.
* For GTM (GTM-XXXXXX): loads gtm.js from the relay subdomain.
*
* The relay subdomain is a customer-owned CNAME that proxies to CloudFront,
* so the browser sees a first-party origin — defeating ad blockers and
* enabling first-party cookies.
*/
function acceptrics_inject_relay_script() {
if (get_option('acceptrics_relay_status', '') !== 'active') {
return;
}
$tag_id = get_option('acceptrics_relay_tag_id', '');
$tag_ids = (array) get_option('acceptrics_relay_tag_ids', []);
if (empty($tag_ids) && !empty($tag_id)) {
$tag_ids = [$tag_id];
}
if (empty($tag_id) || empty($tag_ids)) {
return;
}
$relay_mode = get_option('acceptrics_relay_mode', 'dns');
if ($relay_mode === 'path') {
acceptrics_inject_path_relay_script($tag_id, $tag_ids);
} else {
$hostname = get_option('acceptrics_relay_hostname', '');
if (empty($hostname)) return;
acceptrics_inject_dns_relay_script($tag_id, $tag_ids, $hostname);
}
}
/**
* Emit the GTG spec race-condition prevention script (Script 1).
* Registers all tag IDs in google_tags_first_party before any tag loads.
*/
function acceptrics_gtg_race_condition_script($tag_ids) {
$json = wp_json_encode(array_values((array) $tag_ids));
return '' . "\n";
}
function acceptrics_inject_dns_relay_script($tag_id, $tag_ids, $hostname) {
$safe_host = esc_attr($hostname);
// Script 1: race condition prevention (GTG spec, both GTM and non-GTM).
echo acceptrics_gtg_race_condition_script($tag_ids); // phpcs:ignore WordPress.Security.EscapeOutput
if (strpos($tag_id, 'GTM-') === 0) {
// Script 2: GTM IIFE — no tag ID in the URL per GTG spec.
?>
$relay_base];
$config = [
'strategy' => 'adaptive',
'tagType' => $is_gtm ? 'gtm' : 'gtag',
'primaryTag' => $primary,
'tagIds' => array_values($tag_ids),
'relayBase' => $relay_base,
'primaryRelayUrl' => $relay_base,
'tagPaths' => $tag_paths_config,
'directGtagSrc' => 'https://www.googletagmanager.com/gtag/js?id=' . $primary,
'directGtmSrc' => 'https://www.googletagmanager.com/gtm.js?id=' . $primary,
'degraded' => $degraded,
'routerVersion' => (int) get_option('acceptrics_adaptive_router_version', 1),
'probeTimeoutMs' => (int) get_option('acceptrics_adaptive_probe_timeout_ms', 900),
'directTtlMs' => (int) get_option('acceptrics_adaptive_direct_ttl_s', 86400) * 1000,
'relayTtlMs' => (int) get_option('acceptrics_adaptive_relay_ttl_s', 14400) * 1000,
'statsSampleRate' => (float) get_option('acceptrics_adaptive_stats_sample_rate', 0.10),
'requireFpCookie' => (bool) get_option('acceptrics_adaptive_require_fp_cookie', false),
'consentModeEnabled' => (bool) get_option('acceptrics_consent_mode_enabled', true),
'statsUrl' => admin_url('admin-ajax.php'),
];
$router_file = plugin_dir_path(__FILE__) . 'js/adaptive-router.js';
?>