I recently had a client that wanted to show different Google Analytics code on different pages. I would have had to create different page templates in this client’s WordPress website except I use Genesis which gives me the power to add custom code in different sections and pages of the site. With the plugin, Genesis Simple Hooks I’m able to add some php code to conditionally check what page we’re on and include the code or content on different pages. Nick “the Geek”, a StudioPress Evangelist helped me out with this one (http://designsbynickthegeek.com/tutorials/conditional-page-content) This is a representation of the code that I added in the hook section called wp_head:
<?php if( is_page(63) || is_page(705) || is_page(73) ) { ?> <script type="text/javascript"> ...Google Analytics Code Here </script> <?php } else { ?> <script type="text/javascript"> ...2nd Google Analytics Code Here </script> <?php } ?>
That was all there was to it. We just have to make sure that we encase the PHP code inside the PHP calls. Then we also have to make sure we check “Execute PHP on this hook?”