Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I haven't decided what theme to have on my WordPress site yet.

But I do want Google Analytics on the site.

Before installing Google Analytics on the site, do I need to decide on the theme?

i.e. does the Google Analytics code go into a file that is specific to the theme, or that changes with a changed theme?

Thanks

share|improve this question
add comment

3 Answers

If you add tracking code to a theme file you will need to do this on every theme change.

It is considered good practice to use plugin for this, like Google Analytics for WordPress. It will use common hooks (that every theme worth using has) to insert code and that way you don't need to think about that when changing.

share|improve this answer
add comment

Absolutely you can keep your analytics code Theme-agnostic.

Most analytics code is simply a script, and scripts can (and should) be enqueued using proper hooks. In this case, the relevant hooks require nothing more than Theme support for two all-but-universal template tags: wp_head() and wp_footer().

Keeping the analytics code Theme-agnostic will require writing (or using an existing) Plugin - but not a complicated one, at all. Essentially, it would be simply a hook call, and an associated callback to define the analytics code. Something like this (assuming you've got your analytics code in a file called \js\analytics.js:

<?php
/**
 * Plugin Name: Custom Analytics Code
 */

function someprefix_enqueue_analytics_code() {
    wp_enqueue_script(
        'someprefix_analytics', // script handle
        get_template_directory_uri() . '/js/analytics.js', // script URL
        '', // dependencies; if any: array( dep1, dep2... )
        '', // version number to append to URL query string
        true // output script in the footer
    );
}
add_action( 'wp_enqueue_scripts', 'someprefix_enqueue_analytics_code' );
?>

And that's it!

share|improve this answer
    
Note that Google Analytics code is more complex than single script file to queue. Myself I don't see a reason not to use ready-made plugin with it. –  Rarst Aug 29 '11 at 13:20
    
@Rarst absolutely! If that solution works for a given user, then by all means, use it. :) But the Google Analytics for WordPress Plugin is also doing a LOT more than is, strictly speaking, necessary. (And even with that Plugin, it still boils down to enqueueing and hooking the generated scripts.) –  Chip Bennett Aug 29 '11 at 13:27
add comment

You don't need to pick a theme ahead of time. But when you do pick a theme, be sure it's one that follows WordPress best practices for design (i.e. uses wp_footer() in the appropriate location).

For all of my hosted client sites, I use a mu-plugin to add analytics. This way, they can change their themes whenever they want without losing their analytics. The gist of the plugin is a function and a hook:

function jd_analytics() {
?>
<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    try {
        var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
        pageTracker._trackPageview();
    } catch(err) {}
</script>
<?php
}

add_action('wp_footer', 'jd_analytics');

So long as their theme uses wp_footer(), the analytics scripts will always be added to the page.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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