Add Tracking Code in WordPress

Add Tracking Code in WordPress

July 12, 2023

Meta Pixel Code

The Meta Pixel is a snippet of JavaScript code that allows you to track visitor activity on your website. It works by loading a small library of functions which you can use whenever a site visitor takes an action (called an event) that you want to track (called a conversion).

Google Tag Manager

Google Tag Manager works by using a single JavaScript code snippet that you add to your site as a container for all the tags you want to manage.


function sw_custom_js()
    echo '
    <!-- Your Tracking Code -->

    <!-- End Tracking Code -->';

// Add hook for admin <head></head>
add_action('admin_head', 'sw_custom_js');

// Add hook for front-end <head></head>
add_action('wp_head', 'sw_custom_js');
// Add Google Tag code which is supposed to be placed after opening body tag.
add_action( 'wp_body_open', 'wpdoc_add_custom_body_open_code' );

function wpdoc_add_custom_body_open_code() {
    echo '<!-- Google Tag Manager (noscript) --><noscript><iframe src="; height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><!-- End Google Tag Manager (noscript) -->';


Leave A Comment