• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2662

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

Add Tracking Code To Specific Page

Many different themes provide a custom field for adding tracking code to single posts and pages in WordPress.

Here’s an example of what Genesis offers:

Tracking - Conversion Code

And here’s an example of what Thesis offers:

thesis script

If you’re using a free theme like Twenty Eleven or Twenty Twelve, i’ve got a solution for you abit further on in this post.

Theme With NO Tracking Code Fields

Both the premium theme frameworks above include this field as shown but Not if you install an SEO plugin. When you do this, the default theme SEO fields for adding scripts and tracking code to individual pages and posts deactivates.

So whats the fix?

Genesis

With Genesis, you can install the Simple Hooks plugin and add some php code with a conditional tag and your tracking code/script to the WordPress head hook.

Here’s the sample code you can paste in the field, change the page I.D to the one you want to track and add your tracking code between the html tags.

<?php if (is_page(200) ) { ?><div class="tracking-code">Insert-Code-Here</div><?php } ?>

Thesis

With Thesis, you can install the Thesis Hook plugin and do pretty much the same thing as what you can do with Genesis.

WOO

Woo themes also offer a hook manager built into some of their themes. The hook manager isn’t as flexible as the Genesis and Thesis hook plugins and only executes shortcodes and HTML.

You’ll need to write a custom function or add the code to the header.php if using a Woo theme.

Custom Function Works With All Themes

Here’s the code which works with ALL themes:

add_action( 'wp_head', 'wpsites_add_tracking_code' );
function wpsites_add_tracking_code() {
        if ( is_page(200) ) {
echo'<div class="tracking-code">add your tracking code here.</div>';
        } 
    }

You’ll need to change the page I.D and add your tracking code before pasting this code into your child themes functions.php file. You can also use the: You can add custom functions below section in the Woo themes parent function.php file.

WordPress Hooks

wp_head Executed before the closing head tag
wp_footer Executed before the closing body tag

Change the WordPress hook if you want your tracking code to execute in your footer.

Adding Code To Header.php

If your theme doesn’t offer an easy way to hook in code on specific pages using conditional tags, you can still get the job done fairly easily.

You’ll need to edit your header.php file and add the code i used above for Genesis before the closing head tag.

This method will work with any theme.

Here’s a screenshot of what i’ve tested on the Twenty Twelve default theme for WordPress.

Note the php code with conditional tag and page i.d highlighted in yellow.

Twenty Twelve Theme Header file

Its not advisable to edit your parent theme files so you should copy this file and place it in the root directory of a child theme.

The Twenty Eleven theme and many other free themes will require you to add the code to the header.php file.

Related Tutorials

  • Load Google Analytics Tracking Script From Your Functions File

Reader Interactions

Comments

  1. andrew says

    July 11, 2014 at 8:59 pm

    Hey Brad, When i try to add the custom function to my functions.php file, special characters in my javascript conversion snippet cause php errors and close it out before (‘).

    Do you have any way how to work around this?

    Log in to Reply
    • Brad Dalton says

      July 12, 2014 at 8:18 am

      Hello Andrew

      Use the scripts box if your theme includes one and wrap your script in scripts tags.

      Otherwise, you can use this method http://wpsites.net/tools/how-to-install-buysell-ads-bsa-code-manually-in-any-theme/

      Log in to Reply
  2. Christian Karasiewicz says

    September 20, 2013 at 7:07 pm

    Hi. I am running Genesis, but do not see the custom tracking/conversion code box. Do I need to turn anything additional on to get this to display on a post or page?

    Log in to Reply
    • Brad Dalton says

      September 20, 2013 at 7:09 pm

      Go to Genesis > Theme Settings > Header and Footer Scripts and paste it in there.

      Log in to Reply
      • Christian Karasiewicz says

        September 20, 2013 at 7:13 pm

        I’m adding a script that needs to go on a specific post or page, not site-wide.

        Log in to Reply
        • Brad Dalton says

          September 20, 2013 at 7:25 pm

          The script box for single posts and pages is only available for Genesis 2.0.

          If you’re running Genesis 2.0, you’ll see it below your editor on all edit screens.

          Otherwise, go to the top right hand corner and click screen options where you can check the box to show the field.

          Log in to Reply
          • Christian Karasiewicz says

            September 20, 2013 at 7:27 pm

            Strange…I’m running WP 3.6.1 and Genesis 2.0 and don’t have it.

          • Brad Dalton says

            September 20, 2013 at 7:31 pm

            I’m running 3.7 so maybe that’s why i have it.

            You can load scripts conditionally on one page using code.

            What is the script?

          • Christian Karasiewicz says

            September 20, 2013 at 7:29 pm

            Did it get renamed by chance? Is it now the Scripts box?

          • Christian Karasiewicz says

            September 20, 2013 at 7:32 pm

            Got it figured out. It was renamed to Scripts. Thanks for the help!

  3. Bradley Charbonneau says

    January 31, 2013 at 7:13 pm

    Thanks for the excellent post, Brad! I love WOO Themes but I think their hook manager doesn’t allow PHP so I can’t do the condition PHP for a certain page. I’m currently using this plugin which lets me put code on certain pages, but ugh, yet another plugin. Thoughts?

    Log in to Reply
    • Brad Dalton says

      January 31, 2013 at 11:43 pm

      Hi Bradley. I’ll look for a plugin which works with all themes and let you know. I know adding code directly to the template files isn’t desirable so i need to find a better solution for non Genesis/Thesis users.

      Log in to Reply
    • Brad Dalton says

      February 1, 2013 at 3:49 am

      I’ve written a custom function for you which i tested on Woo’s Canvas theme. You’ll need to replace the page I.D and add your tracking code before adding this to your child theme.

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems