'tracking_codes',
'title' => 'Tracking & analytics codes',
'desc' => 'Add Google Tag Manager, GA4, Meta Pixel and TikTok Pixel without another plugin',
'icon' => 'code',
'risk' => 'safe',
'level' => 'standard',
'audience' => 'business',
'priority' => 1,
'benefit' => 'Keeps the common tracking tags in one place, so you do not need a separate plugin for each one.',
'help' => 'Enter your tracking IDs and enable each service. A tag is only added when its service is enabled and the ID has the right format. Tags load asynchronously in the page head for every visitor, including logged-in users; exclude your own visits with the analytics tool\'s filters. If both GTM and GA4 are enabled, only GTM is loaded (add GA4 inside GTM). This feature does not ask visitors for consent: these services set cookies, so make sure your cookie consent setup covers them.',
'before' => 'A separate plugin or code snippet for each tracking service',
'after' => 'IDs entered here; tags added from one place.',
'extras' => array(
// Google Tag Manager.
array(
'key' => 'track_gtm_enable',
'type' => 'bool',
'label' => 'Enable Google Tag Manager',
),
array(
'key' => 'track_gtm_id',
'type' => 'text',
'pattern' => '/^GTM-[A-Z0-9]{4,12}$/',
'label' => 'GTM Container ID',
'placeholder' => 'GTM-XXXXXXX',
'desc' => 'Find it in tagmanager.google.com: open your container; the ID (GTM-XXXXXXX) is shown at the top of the workspace.',
'default' => '',
),
// Google Analytics 4.
array(
'key' => 'track_ga4_enable',
'type' => 'bool',
'label' => 'Enable Google Analytics 4',
),
array(
'key' => 'track_ga4_id',
'type' => 'text',
'pattern' => '/^G-[A-Z0-9]{4,16}$/',
'label' => 'GA4 Measurement ID',
'placeholder' => 'G-XXXXXXXXXX',
'desc' => 'Find it in analytics.google.com → Admin → Data streams → your web stream → Measurement ID. Skip this if you use GTM: while GTM is enabled this tag is not loaded, so add GA4 inside GTM instead.',
'default' => '',
),
// Meta Pixel.
array(
'key' => 'track_meta_enable',
'type' => 'bool',
'label' => 'Enable Meta (Facebook) Pixel',
),
array(
'key' => 'track_meta_pixel',
'type' => 'text',
'pattern' => '/^[0-9]{10,20}$/',
'label' => 'Meta Pixel ID',
'placeholder' => '123456789012345',
'desc' => 'Find it in Meta Events Manager: select your pixel (dataset); its ID is a long number. Enter digits only.',
'default' => '',
),
// TikTok Pixel.
array(
'key' => 'track_tiktok_enable',
'type' => 'bool',
'label' => 'Enable TikTok Pixel',
),
array(
'key' => 'track_tiktok_pixel',
'type' => 'text',
'pattern' => '/^C[A-Z0-9]{10,30}$/',
'label' => 'TikTok Pixel ID',
'placeholder' => 'CXXXXXXXXXXXXXXXXX',
'desc' => 'Find it in TikTok Ads Manager under Events, on your web pixel. The ID starts with C.',
'default' => '',
),
),
'callback' => function () {
$ids = array(
'gtm' => ovkit_get_setting( 'track_gtm_enable', 0 ) ? (string) ovkit_get_setting( 'track_gtm_id', '' ) : '',
'ga4' => ovkit_get_setting( 'track_ga4_enable', 0 ) ? (string) ovkit_get_setting( 'track_ga4_id', '' ) : '',
'meta' => ovkit_get_setting( 'track_meta_enable', 0 ) ? (string) ovkit_get_setting( 'track_meta_pixel', '' ) : '',
'tiktok' => ovkit_get_setting( 'track_tiktok_enable', 0 ) ? (string) ovkit_get_setting( 'track_tiktok_pixel', '' ) : '',
);
// Output only IDs that are well-formed, whatever is stored.
$patterns = array(
'gtm' => '/^GTM-[A-Z0-9]{4,12}$/',
'ga4' => '/^G-[A-Z0-9]{4,16}$/',
'meta' => '/^[0-9]{10,20}$/',
'tiktok' => '/^C[A-Z0-9]{10,30}$/',
);
foreach ( $ids as $service => $id ) {
$id = strtoupper( trim( $id ) );
$ids[ $service ] = preg_match( $patterns[ $service ], $id ) ? $id : '';
}
// Register a third-party tag: the vendor's script loads async, and the
// small configuration snippet that queues the first calls is attached
// with wp_add_inline_script() so it runs before the tag.
$add_tag = function ( string $handle, string $src, string $config ) {
// Third-party tags are versioned by the vendor; a ver query argument would only bust their CDN cache.
wp_register_script( $handle, $src, array(), null, array( 'strategy' => 'async' ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Vendor-versioned third-party tag.
wp_add_inline_script( $handle, $config, 'before' );
wp_enqueue_script( $handle );
};
// No PHP admin exclusion: page caches serve the same HTML to every
// visitor. Exclude admin traffic with the analytics tool's own filters.
add_action(
'wp_enqueue_scripts',
function () use ( $ids, $add_tag ) {
if ( $ids['gtm'] ) {
$add_tag(
'ovkit-gtm',
'https://www.googletagmanager.com/gtm.js?id=' . rawurlencode( $ids['gtm'] ),
"window.dataLayer=window.dataLayer||[];window.dataLayer.push({'gtm.start':new Date().getTime(),event:'gtm.js'});"
);
}
// GA4 standalone only when GTM is not loaded, to avoid double counting.
if ( $ids['ga4'] && ! $ids['gtm'] ) {
$add_tag(
'ovkit-ga4',
'https://www.googletagmanager.com/gtag/js?id=' . rawurlencode( $ids['ga4'] ),
'window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag("js",new Date());gtag("config",' . wp_json_encode( $ids['ga4'] ) . ');'
);
}
if ( $ids['meta'] ) {
$add_tag(
'ovkit-meta-pixel',
'https://connect.facebook.net/en_US/fbevents.js',
'!function(f){if(f.fbq)return;var n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version="2.0";n.queue=[];}(window);'
. 'fbq("init",' . wp_json_encode( $ids['meta'] ) . ');fbq("track","PageView");'
);
}
if ( $ids['tiktok'] ) {
$add_tag(
'ovkit-tiktok-pixel',
'https://analytics.tiktok.com/i18n/pixel/events.js?sdkid=' . rawurlencode( $ids['tiktok'] ) . '&lib=ttq',
'!function(w,t,id){w.TiktokAnalyticsObject=t;var ttq=w[t]=w[t]||[];'
. 'ttq.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie","holdConsent","revokeConsent","grantConsent"];'
. 'ttq.setAndDefer=function(o,m){o[m]=function(){o.push([m].concat(Array.prototype.slice.call(arguments,0)))}};'
. 'for(var i=0;i fallbacks belong in .
add_action(
'wp_body_open',
function () use ( $ids ) {
$allowed = array(
'noscript' => array(),
'iframe' => array(
'src' => true,
'height' => true,
'width' => true,
'style' => true,
'title' => true,
),
'img' => array(
'src' => true,
'height' => true,
'width' => true,
'style' => true,
'alt' => true,
),
);
if ( $ids['gtm'] ) {
echo wp_kses( '', $allowed ) . "\n";
}
if ( $ids['meta'] ) {
echo wp_kses( '', $allowed ) . "\n";
}
},
1
);
},
);