Google Analytics tracking code on just one page?

Having an issue where I have a client who wants his Google Analytics code on just one page. I'm using the following is_page code but in the footer.php of my child theme, but it's not appearing on the page. Is this code correct?

?php if ( is_page( 'contact' )) {
    echo 'script type="text/javascript"/* ![CDATA[ */
var google_conversion_id = 000000000;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "000000000";
var google_remarketing_only = false;
/* ]] *//script
script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"/script
noscriptdiv style="display:inline;"img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/000000000/?label=000000000amp;guid=ONamp;script=0"//div/noscript';
} ?

Topic footer conditional-tags google-analytics pages Wordpress

Category Web


I'd suggest that you simply use some custom field to add a flag "track this post". Let's say you use a name of ga_tracking_enabled.

In your footer.php, you could then just check this post meta data to enable tracking:

if ( get_post_meta( get_the_ID(), 'ga_tracking_enabled', true ) ) {
    // GA tracking code goes here
}

You can add this a bit more user friendly by adding a small plugin that adds a custom meta box with a switch to set the ga_tracking_enabled field to true.

Important: 1. Do NOT use a specific Post ID as this is not guaranteed to stay (think: moving your database) 2. and do NOT use a specific page slug as if there's already a post with this URl, you'll get -2, etc. appended automatically, breaking your change again.


If you have mywebsite/contact as url it should appear in the footer. Make sure the slug is "contact" or use page ID as suggested by Zakir. You can find the page ID by going to Pages in the admin area and hover over the contact page. You will see it then on the bottom left of your screen in the url mywebsite/wp-admin/post.php?post=10$action=edit --> post id = 10


Instead of page slug use page id like is_page(12) where page id 12

Edit Your code should work I afraid your page slug is not same, so it will be safe to use page ID instead of slug

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.