• Resolved dsb0328

    (@dsb0328)


    I was wondering what the best solution was to enter the Google Analytics code so that every page visit is collected for Google Analytics. I found this result, but it’s like 2 years old and wasn’t sure if it was up to date. I was also told to put the code in the header.php file just before the </head> line, but there is no </head> line in the header.php file.

    On another note related to the header.php file, I have this Error on my Editor page, and don’t know how to resolve it since there is no </head> line in the header.php file. But the site seems to work okay.

    Error : Please fix your theme to make plugins work correctly: Go to the Theme Editor and insert <?php wp_head(); ?> just before the </head> line of your theme’s header.php file.

    Any help would be greatly appreciated. Especially with the Google Analytics.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter dsb0328

    (@dsb0328)

    Also, would it be best to just use a plugin? If so, does anyone have any good luck with any of them?

    I’d use a plugin. Yoast‘s is popular.

    Theme Author presscustomizr

    (@nikeo)

    Hi, We’ve just published a new documentation about this : http://docs.presscustomizr.com/article/179-setting-up-google-analytics-with-the-customizr-theme

    Hope it helps

    Thread Starter dsb0328

    (@dsb0328)

    Thanks for the info guys.

    So I went ahead and tried to put the code into my child theme functions.php file but it didn’t work. I have other code in there already creating an extra widget and adding a wrap div, but I don’t think that would be the problem. I tried to place the code above that, but below the initial <?php line. I removed the first and last line (<?php and ?>) because it was undoing (removing all color from lines of code) the rest of my code below it and giving me a syntax error for the just pasted <?php line. Went and checked GA and it said it wasn’t installed. Waited about 10 minutes and still wasn’t installed. Am I doing something wrong here? Or should I wait longer for Google to see it? (I know CSS pretty well, but java is pretty foreign to me.)

    I’m fine with adding another plugin, but I’d hate to bloat the site more than I have to with another plugin if it’s a simple fix.

    i also use yoast is very good and you just paste the metadata you get from google.

    Thread Starter dsb0328

    (@dsb0328)

    When you all use Yoast, do you manually enter in the UA code? I have a few different gmail accounts with different analytics in each one. If I authenticate with my Google account, will that screw anything up if I sign in and out to Google? I think I’d prefer the manual way, but it says you can’t use the dashboards that way.

    Thread Starter dsb0328

    (@dsb0328)

    Okay, I’ve tried the code in the functions.php method, Google Analyticator, and Google Analytics by Yoast. And none of them work. I’m pretty sure it’s user error, so I am looking for any kind of help here.

    I am going to try to let the functions.php file, with the code the way I mentioned earlier, sit overnight to see if Google sees it. Obviously with the 2 plugins deactivated. (btw, I only tried each of these methods 1 at a time.)

    Please help if you can. I’m hoping that since nikeo is the theme author, he might have some more advice.

    Hi,
    can you show a screenshot or pastebin the contents of your functions.php?

    Thread Starter dsb0328

    (@dsb0328)

    Here is all of the code in my functions.php:

    <?php
    
    // Add Google Analytics
    
      add_action('wp_head','my_analytics', 20);
      function my_analytics() {
        ?>
          /* START OF TRACKING 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'); /* CHANGE THE TRACKING ID HERE */
            ga('send', 'pageview');
          </script>
          /* END OF TRACKING CODE */
        <?php
      }
    
    // Adds a widget area.
    if (function_exists('register_sidebar')) {
     register_sidebar(array(
     'name' => 'Extra Header Widget Area',
     'id' => 'extra-widget-area',
     'description' => 'Extra widget area after the header',
     'before_widget' => '<div class="widget my-extra-widget">',
     'after_widget' => '</div>',
     'before_title' => '<h2>',
     'after_title' => '</h2>'
     ));
    }
    
    // Place the widget area after the header - only on home page
     add_action ('__after_header', 'add_my_widget_area', 0);
     function add_my_widget_area() {
     if ( (function_exists('dynamic_sidebar')) && (tc__f('__is_home')) ) {
     dynamic_sidebar('Extra Header Widget Area');
     }
    }
    
    /* Add a wrap div to the whole content except the footer */
    
    add_action( '__before_header' , 'my_wrap_start' );
    function my_wrap_start(){
        echo '<div id="wrap">';
    }
    
    add_action( '__before_footer' , 'my_wrap_end' );
    function my_wrap_end(){
        /* close #wrap and add a useful div avoiding the footer
         * overlaps the page content under certain circumstances
         */
        echo '<div id="push"></div></div><!--#wrap-->';
    }
    
    add_action('wp_footer', 'sticky_footer', 9999);
    function sticky_footer(){?>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                ! function($){
                    "use strict";
                    var $wrap = $('#wrap'),
                        $footer = $('footer#footer'),
                        $reset_margin_top = $('#tc-reset-margin-top'),
                        $html = $('html'),
                        $body = $('body'),
                        $push = $('#push'),
                        isCustomizing = $('body').hasClass('is-customizing'),
                        isUserLogged = ! isCustomizing && ( $('body').hasClass('logged-in') || 0 !== $('#wpadminbar').length ),
                        stickyEnabled = $body.hasClass('tc-sticky-header'),
                        resizeBodyHtml = isUserLogged || stickyEnabled,
    
                        $window_width = $(window).width();
    
                    function resize_body_html(timeout){
                        if ( ! resizeBodyHtml )
                            return;
    
                        setTimeout(function(){
                            if ( isUserLogged ){
                                $html.css('height', '100%');
                                $html.css('height', parseInt($html.css('height')) - $('#wpadminbar').height() );
                            }
    
                            if ( stickyEnabled ){
                                $body.css('height', '100%');
                                $body.css('height', parseInt($body.css('height')) - parseInt( $reset_margin_top.css('marginTop') ) );
                            }
                        }, timeout ) ;
                    }
    
                    function render_sticky_footer(){
                        var $push_height = parseInt( $footer.css('height') ) + parseInt( $footer.css('borderTopWidth') ) + parseInt( $footer.css('borderBottomWidth') ) + 1,
                            $wrap_b_margin = -1 * $push_height;
    
                        $wrap.css('marginBottom', $wrap_b_margin );
                        $push.css('height', $push_height );
                    }
    
                    render_sticky_footer();
                    resize_body_html(50);
    
                    $(window).resize(function(){
                        setTimeout( function(){
                            // re-render on resizing only if an "interesting" resing occurred
                            if ( Math.abs($(window).width() - $window_width) > 50 ){
                                render_sticky_footer();
                                $window_width = $(window).width();
                            }
                            resize_body_html(50);
                        }, 100);
                    });
                }(window.jQuery);
             });
        </script>
    
    <?php
    }
    Thread Starter dsb0328

    (@dsb0328)

    Oh, but wait. Google Analytics is now saying Status: Receiving Data. I’m going to keep an eye on it today to make sure everything is okay.

    However, if my code is correct, might as well leave it up there in case anyone else needs to see how to add the code when they already have other code in their functions.php file.

    I’ll update with the analytics progress when I feel it is working well. If you see anything funky, please feel free to comment.

    Thanks again everyone!

    Thread Starter dsb0328

    (@dsb0328)

    Okay, so the status in GA is Receiving Data but I am not seeing any page hits when I view it from my other laptop that is not logged in to WordPress. And I am not seeing any Active Users in the Real Time Overview area. Any ideas?

    Another note about the code, I removed the /* START OF TRACKING CODE */ and /* END OF TRACKING CODE */ lines because they were showing up above my navbar in a grey row.

    Thread Starter dsb0328

    (@dsb0328)

    Okay. I’m an idiot. I didn’t put my GA Tracking ID in the code. I think it’s working now. At least I see myself as a Real Time visitor while I’m looking at it anyway.

    Thanks again for your help!

    Hi,
    You did right by removing /* START OF TRACKING CODE */ and /* END OF TRACKING CODE */. Those are just indications.
    Great to know it is working!

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

The topic ‘Google Analytics – Best Option’ is closed to new replies.