Add a tracking code to your WordPress theme footer

If you want to add for example a Google Analytics or Piwik tracking code to your theme, you can either just add it directly in your footer.php or add it via functions.php. Here is how to do the latter:

Add this to your functions.php file:

/*  Tracking code
/* ------------------------------------ */
function alx_tracking_code() {
	echo 'My tracking code goes here'."\n";
add_filter( 'wp_footer', 'alx_tracking_code' );

All that this does is that it echoes out the code via wp_footer(). So make sure that your theme has the following just before the closing of the body tag in footer.php:

<?php wp_footer(); ?>

Function reference: wp_footer

Written by:Alexander Agnarson

Leave a Reply

Your email address will not be published. Required fields are marked *