conditional tags - Google Analytics tracking code on just one page?

admin2025-01-08  6

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/pagead/conversion.js"></script>
<noscript><div style="display:inline;"><img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices/pagead/conversion/000000000/?label=000000000&amp;guid=ON&amp;script=0"/></div></noscript>';
} ?>

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>';
} ?>
Share Improve this question edited Mar 3, 2019 at 9:57 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 14, 2015 at 23:11 Adam BellAdam Bell 1719 bronze badges 2
  • 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 Commented Mar 3, 2019 at 9:56
  • The easiest way is to use the plugin SOGO Add Script to Individual Pages Header Footer – user179769 Commented Dec 18, 2019 at 9:09
Add a comment  | 

3 Answers 3

Reset to default 1

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

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266089a1092.html

最新回复(0)