• I use a child theme. I understood in this case the google code has to be inserted in the funcionts.php file. Therefore I realized I have two of this file. The first in Themes folder, the second in Theme-child folder.

    Which one of the two?

    Then Google recommend to use this code:
    <script>
    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

    ga(‘create’, ‘UA-XXXXXXXX-X’, ‘auto’);
    ga(‘send’, ‘pageview’);

    </script>

    I’ll add this code at the end of the file after } but before ?> ???

    Then it seems someone use only

    add_action(‘wp_footer’, ‘ga’);
    function ga() { ?>
    UA-XXXXXXXX-X
    <?php } ?>

    Is it correct the code?

    Thanks in advance!!!!!! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • I use a child theme…the google code has to be inserted in the functions.php file…first in Themes folder, the second in Theme-child folder.

    Which one of the two?

    I think either or both would be fine, and with only either being necessary. As I understand things, functions.php in the Parent Theme is primary and is *always* called, then WordPress also reads functions.php from the Child Theme *if present*. So again, either location should work the same.

    Then Google recommend to use this code…

    I’ll add this code at the end of the file after } but before ?> ???

    Yes. That last } you have mentioned is a closing bracket for something ahead of it, then the ?> marks the end of PHP and is actually optional altogether. However, you do want to be certain nothing at all ever follows that ?> closing tag (unless PHP is then also re-started) or else PHP will stall and crash.

    Then it seems someone use only

    add_action('wp_footer', 'ga');
    function ga() { ?>
    UA-XXXXXXXX-X
    <?php } ?>

    Is it correct the code?

    That question is beyond what I know, so maybe someone else has an answer for you.

    Thread Starter clage

    (@clage)

    Thank you leejosepho!:-)

    It would be great if someone have an answer for the second half!

    You could name this file something like myga01.php and put it in a folder having a name something like my-google-analytics and then upload that folder into /wp-contents/plugins/ to test it by simply activating it as well as to then also have your own custom plugin with that code (or any other code) separate from your theme just in case you might ever want to switch themes without having to remember to copy and move your code…

    <?php
    /*
    Plugin Name: My Google Analytics
    Description: Google Analytics add_action('wp_footer', 'ga');
    More info: <a href="http://www.google.com/analytics/" href="http://www.google.com/analytics/" target="_blank">Google Analytics</a>.
    */
    add_action('wp_footer', 'ga');
    function ga() { ?>
    UA-XXXXXXXX-X
    <?php } ?>

    http://codex.wordpress.org/Writing_a_Plugin

    https://www.google.com/search?q=how+to+make+a+wordpress+plugin

    Thread Starter clage

    (@clage)

    Thanks again! This is an idea! I like the idea to become independent by the update of the themes without remember every time to add some code but I have only one concerning because it should be a plugin, and I read that too many plugins could make the blog slower and heavier. Which is your experience about?

    I read that too many plugins could make the blog slower and heavier. Which is your experience about?

    Too much code doing too much stuff too inefficiently and/or all at once or incorrectly or in a non-orderly fashion is what bogs things down, not whether or not any particular code might happen to be located at /wp-content/plugins/xyz/somebodys.php. Placing your GA code in a plugin *might* have some kind of effect on where it stands in the call order — just guessing here — but I would suspect plugins at least get called immediately after the theme anyway.

    Digging in a bit further, maybe take a look and possibly re-set certain cron jobs to run a few minutes apart (rather than simultaneously) or even a bit less frequently:

    https://wordpress.org/plugins/wp-crontrol/

    Edit: Every time I try a cache plugin, my sites slow down!

    Thread Starter clage

    (@clage)

    I uploaded it! It’s ok! I’ll keep you informed

    Please do!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘google analytics and child theme’ is closed to new replies.