Can I install Analytics before deciding on a theme?

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

But I do want Google Analytics on the site.

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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 jsanalytics.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!

  3. 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.