Adding tracking codes and scripts to functions.php

Hi there, I have been having difficulty knowing the correct method to insert various tracking codes immediately above the head tags and also other tracking codes that need to go just below the body tags. I know that it needs to go in the child theme’s function.php file but I’m unsure of the correct accompanying code that is required for it to work. I’ve seen several methods on the forum which made me confused as to the best approach.

The method that I am using for the tracking code that is required to go above the head tag is this (I am specifically using it for Facebook pixel code, Google Optimize tracking code and VWO split testing code):

// Add scripts to wp_head()
function child_theme_head_script() { ?>

	<!-- I've put the facebook pixel code here -->
	<!-- I've put the google code here -->
	<!-- I've put the vwo code here -->

<?php }

add_action( 'x_before_site_begin', 'child_theme_head_script' );

With the tracking code in place using the above method, all seems to be well in terms of the site still working, although I’m not sure if the tracking code is actually working yet until I hear back from the client. I just want confirmation from you that I have indeed used the correct method for putting code above the head tag. Is this correct?

The main problem on the other hand is how do I put tracking code below the body tag? I specifically need to insert a tracking code for Ontraport which specifies to be inserted below the body tag. On the Apex forum I found this code which I inserted in the functions.php below the header code that I’ve just mentioned:

// Add PHP Code after body tag

    function my_custom_code() { 
    // Add your custom code here
     }

    add_action('x_before_site_begin', 'my_custom_code');

Problem is that whenever I add the tracking code it breaks the site. However, the site doesn’t break if I don’t add the tracking code and simply leave it with the ‘add your custom code here’ which made me think the tracking code was at fault. I then tried another tracking code and it also broke the site, so I think I must be adding it incorrectly. Please could you advise me on the correct method to adding tracking code below the body tag without breaking the site.

Many thanks
Lewis

Hi there,

I suggest that you use the standard actions of the WordPress instead. For the header you can use this code:

function third_party_tracking_code_header() { ?>

  <script>
    // Third party tracking code.
  </script>
<?php }
add_action( 'wp_head', 'third_party_tracking_code_header' );

Add your code instead of // Third party tracking code.. For the end of the body:

function third_party_tracking_code_footer() { ?>

  <script>
    // Third party tracking code.
  </script>
<?php }
add_action( 'wp_footer', 'third_party_tracking_code_footer' );

For more information:
https://theme.co/apex/forum/t/customizations-actions-and-filters-in-x/208

If the last code is still breaking your website, you need to check the code you add itself as it might be the problem cause.

Thank you.

Thanks for your help!

Glad we were able to help :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.