With tracking codes and pixels able to help you track conversions and the successes of your marketing efforts, these will need to be added to each page of your site to track user journey and behaviours.
While there are plugins to achieve being able to do this, I always prefer to build this into my own custom plugin or theme, via the functions.php file.
Find below the code to add in order to add these codes, or any codes into your
Add code to your WP Head
function prohead_function() {
echo 'add your head code in here';
}
add_action('wp_head', 'prohead_function');
Add code to your WP opening body
function probody_function() {
echo 'add your head code in here';
}
add_action('wp_body_open', 'probody_function');
Now this method above only works when your theme utilises the wp_body_open(); function. If it doesn’t work, you may need to add the following into your template immediately after the opening body tag:
<?php wp_body_open(); ?>
Add code to your Footer
function profooter_function() {
echo 'add your footer code in here';
}
add_action('wp_footer', 'profooter_function');
Alternatively, use ACF
Another method to do this if your clients or customers want the ability to add or edit these codes at any time, is to use Advanced Custom Fields Pro fields to an options page and add the fields into your template to make them CMS editable.
Once Advanced Custom Fields Pro is installed, you can use an Options page to create new fields like so:
Using the example above, you can call your fields in like this:
// add this into the head.php / header.php where you want the tags to show
<?php if( get_field('head_tags','options') ){ ?>
<?php the_field('head_tags','options'); ?>
<?php } ?>
// add this into the header.php or page templates where you want the tags to show
<?php if( get_field('body_tags','options') ){ ?>
<?php the_field('body_tags','options'); ?>
<?php } ?>
// add this into the footer.php where you want the tags to show
<?php if( get_field('footer_tags','options') ){ ?>
<?php the_field('footer_tags','options'); ?>
<?php } ?>
And voila, you can add any tags using these 2 methods to add any tracking codes or pixels easily to any theme.