0

I'm doing up a WordPress theme at the moment, and I want users to be able to add Google Analytics to their site by just adding their tracking code -- you know, the UA-20149670-1 -type number thing.

At the moment I have all the Google Analytics JavaScript code sitting in the <head> of my pages, with the tracking code being set by the user via a theme option text input form.

However, if the user doesn't add a tracking code, then all the Google JavaScript is still left sitting in the <head>, without a tracking code, bloating up the header.

How can I code this so that the Google Analytics code only appears after the user puts in their tracking code?

Additionally, at the moment the Google Analytics code is all still in header.php -- ideally I'd like to move it instead to functions.php. How best would I do that?

Thanks.

1
  • Could we see how your code currently looks, just the code that deals with output for GA, you should only need a simple IF condition around the code.
    – t31os
    Mar 19, 2011 at 15:22

2 Answers 2

3

Take the GA code from your header and wrap it in a function hooked to wp_head;

function __analytics_head()
{
    $options = get_option( 'themename_theme_options' );
    if ( !empty( $options['analytics'] ) ) :
    ?>
<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '<?php echo esc_js( $options['analytics'] ) ; ?>']);
    _gaq.push(['_trackPageview']);
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>
    <?php
    endif;
}
add_action( 'wp_head', '__analytics_head', 100 );

Obviously, this is only guideline code - the way you retrieve your options may vary - but the idea is to check if a GA account has been set, and if so, proceed to print the script.

5
  • Hi TheDeadMedic, thanks for this. I gave it a go and unfortunately it doesn't work at the moment: WordPress chokes on it and only loads the stuff before it in the <head>, then stops dead. I have very little PHP knowledge, so I didn't really know what, if anything, to do next, but maybe you or someone else here could have a go at updating this? It certainly seems like it's along the right lines for what I'm looking for. Thanks.
    – snthr
    Apr 7, 2011 at 14:58
  • I have revised my answer with a more concise solution. It was most likely choking as you hadn't swapped out my example function get_my_theme_option() with a valid one. For now I have resorted to get_option(). Apr 8, 2011 at 10:22
  • Thanks again. It's still not really working, but I think we're almost there... :) I'm using the Themeshaper sample theme options page and usually getting stuff like this: $options = get_option('themename_theme_options'); echo $options['analytics']; -- does that imply any changes for your code?
    – snthr
    Apr 8, 2011 at 13:43
  • Revised answer again. Apr 9, 2011 at 10:28
  • And we're working. :) Thankyou, TheDeadMedic!
    – snthr
    Apr 9, 2011 at 13:55
-5

on a sidenote; the Analytics code should be sitting in the footer...

4
  • 2
    That is wrong. Google changed there code about a year ago to asynchronous script so it should go inside the head tag
    – Bainternet
    Mar 19, 2011 at 14:42
  • from Google Analytics Help: "...Once you find the code snippet, copy and paste it into the bottom of your content, immediately before the </body> tag of each page you are planning to track..."
    – stffn
    Mar 19, 2011 at 14:50
  • 2
    As @Bainternet said, for asynchronous (the latest), it's </head> - google.com/support/analytics/bin/answer.py?answer=174090 Mar 19, 2011 at 15:06
  • 2
    Hey @stffn, this would be better left as a comment on the question because it doesn't directly answer the question.
    – nobody
    Mar 19, 2011 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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