1

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>
<noscript><div style="display:inline;"><img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/000000000/?label=000000000&amp;guid=ON&amp;script=0"/></div></noscript>';
} ?>
| improve this question | | | | |
  • Can you please let me know on which only one page you want to display this Google Analytics code means you can share with me page title, page id and page slug so I can help you with correct code. – Tanmay Patel Mar 3 '19 at 9:56
  • The easiest way is to use the plugin SOGO Add Script to Individual Pages Header Footer – daniloviz Dec 18 '19 at 9:09
0

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

| improve this answer | | | | |
0

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

| improve this answer | | | | |
0

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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.