• Resolved dustylover

    (@dustylover)


    Where in the functions.php do I put the code for Google Analytics?

    I see there’s an action script that has to go in as well. Where am I supposed to put it in?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hello,
    I think you should try to paste your analytics code in the header.php file just before the </header> closing tag!(line 33 for the (great) customizr theme)
    Cheers!

    It isn’t a good practice to edit theme files as an update will undo your changes. Instead use a plugin to do this or create your own one!

    It’s simple, create and edit a file in the following location

    wp-content/plugins/google-analytics.php

    and place the following code.

    <?php
    /*
    Plugin Name: Google Analytics
    Description: Adds Google Analytics
    Author: me
    Version: 1.0
    */
    
    add_action( 'wp_head', 'add_ga_script' );
    
    function add_ga_script()
    {
    ?>
    <script type="text/javascript">
    //Your Google Analytics script here
    </script>
    <?php
    }

    You’ll find a new plugin in the admin area. Activate it and you’re good to go.

    I’d have recommended a Child Theme as the best approach. The code would simply be added to the functions.php file in the Child Theme.

    Thread Starter dustylover

    (@dustylover)

    Just so everyone knows, I am using a child theme, not the actual theme.

    Thread Starter dustylover

    (@dustylover)

    Resolved.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What was the solution?

    Thread Starter dustylover

    (@dustylover)

    The actual functions.php was in the google analytics plugin. I also had to enter my Analytics Id into the WP dashboard where it says Google Analytics.

    But for those of you who want to try a test run to see if this all works, go to http://www.cpwebsite.com, and I can then look at it on Google Analytics to see if it all is working right.

    If anybody wants to see it, and suggests we reopen this just in case there is a continued problem, here it is.

    [ Plugin source redacted, it’s not necessary to paste that here. http://wordpress.org/extend/plugins/googleanalytics/ ]

    Using a child theme is also good solution but in case the user decides to switch themes he/she has to remember to copy the code.

    So if a plugin is used this won’t be a problem.

    Thread Starter dustylover

    (@dustylover)

    I replaced the UA property from the zeroes to my actual UA number.

    Thread Starter dustylover

    (@dustylover)

    I’m unresolving the problem so we can work on this further.

    Thread Starter dustylover

    (@dustylover)

    What I did was insert my actual UA code into the Google Analytics plugin inside Dashboard, and then inserted my UA code in analytics.php where the number had a set of zeroes after the letters UA. I hope that helps.

    @dustylover, if you’re not averse to plugins then you could try this which handles it all for you.

    @jesin, thanks for explaining your logic, I understand. Guess I don’t know why anyone would want to choose a different Theme 😉

    Thread Starter dustylover

    (@dustylover)

    I managed to resolve the problem finally, and my Google Analytics are working for the most part. I do have the Yoast SEO plugin, so I’m not uncomfortable using their products at all.

    <?php
    add_action(‘wp_footer’, ‘add_googleanalytics’);
    function add_googleanalytics() { ?>
    // Paste your Google Analytics code here
    <?php } ?>

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Where in the functions.php do I put the code for Google Analytics’ is closed to new replies.