1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Google Analytics .PHP placement problem

Discussion in 'Google Analytics' started by Mr.Dog, May 6, 2015.

  1. #1
    Hello,

    I have .php pages and I placed the Analytics code into an external file, as directed in the Google Analytics program.

    The external file runs through "include" on every page.

    Problem is: initially I had it placed right after the start of <BODY>, then I moved the 'include' before the end of </HEAD>. And now I'm confused. Not sure which one is better.
     
    Mr.Dog, May 6, 2015 IP
  2. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #2
    Hi,

    we always put our analytics at the very bottom of the page - just before closing body tag </body>. The logic here is that you probably do not want to count visitors that did not even loaded the full page, right? Also, analytics code should be the last one that the browser is bothered with ("it's just stats, your primary content is more important").
     
    O-D-T, May 7, 2015 IP
  3. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #3
    What you're saying is a 3rd variant.

    First, Google said it's best before the end of the </HEAD>, but the PHP indication says right after <BODY> is best.

    I'm speechless, lost.

    I think my Analytics is not loading well or something, because I'm tracking way too few visitors and I know through other programs that I have a ton more (and those are not bots/spiders, but real people).
     
    Mr.Dog, May 7, 2015 IP
  4. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #4
    Yes, you are right, Google does recommend that. There might be reasons for that if you have additional scripts that rely on Google Analytics. If you do have only GA and no dependencies, it will work just before end body tag as well. We use it like that for ages. It should not really be a problem to have it in head if you load GA script asynchronously. That way it won't slow down your site.

    But the problem you are dealing might not be caused because of bad placement. It might be a configuration problem, in which case you should check that you have the correct code snippet and correctly setup your GA account. On the other hand, if it works for some of the users, it suggest that the problem might be that not all users actually load that code. So, you should review whether all pages on your web really include that code.

    I know these are just general advices, but without seeing the actual setup, it is hard to say more.
     
    O-D-T, May 7, 2015 IP
  5. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #5
    What does it mean to "load asynchronously"?

    It's there in the code, it loads from an external .php file.

    I replaced the code and there were some minor differences between the new GA code and the old one. Not sure whether that was the problem.

    Some pages on my site are buried in folders, so I load the code "jumping" up on folders - SOURCE:

    "../../analyticscode.php"

    or even:

    "../../../analyticscode.php"
     
    Mr.Dog, May 7, 2015 IP
  6. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #6
    Your GA code can either be synchronous version or asynchronous. This is not about how you load in with PHP, but which code is in the final PHP file analyticscode.php
    I am not sure if Google still supports synchronous version, but there used to be two versions - synchronous and asynchronous and you could choose which one you would like to install to your pages.
    If your GA code contains something like "async=1" then it is asynchronous.

    As for loading in PHP - if you are sure that all these "jumps" are done always correctly, you should be fine. I would check that from the end user view - load various pages of your web to the browser and check that the code is there.
     
    O-D-T, May 7, 2015 IP
  7. Adrian Robertson

    Adrian Robertson Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Loading the GA code asynchronously means that the code is loaded (or script run) and the rest of the HTML can be rendered at the same time. It doesn't wait for the script to stop running first.

    In the "old days" it was often recommended to load the GA code at the bottom of the site, so that all the page loads first, and then the GA stat is counted. When the GA code was synchronous and if you had it in the <head> section, if the browser got stuck loading it then you would never actually see your page (as it would wait to complete loading the script before rendering anything).

    The more modern way is to use asynchronous code, which doesn't wait to be loaded first (hence it is safe to place it in the Google recommended place, being the <head>). Of course, if someone clicked your site and clicked away almost immediately, you might still score a GA hit (as others have mentioned in this thread).

    The two styles of GA code are quite different, but I find the easiest way to tell is to look at the .js part of the code.
    If you can see ga.js, then it is the older, synchronous style of code (and therefore better placed just before the closing body tag).
    If you see analytics.js, it is the newer asynchronous style of code, and safe to place in the <head>.

    Hope that helps.
     
    Adrian Robertson, May 18, 2015 IP
  8. abhijitnumber1

    abhijitnumber1 Member

    Messages:
    19
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    38
    #8
    Putting your code at the very end is good practice, because when browser reads data to display. It's actually read it one by one. First <!DOCTYPE html>, then <html> then <head> then next etc.

    So, if you put your analytics code at the very start, the analytics code can cause some problem in page loading. because, browser will read analytics code first and then the rest of the page content.
     
    abhijitnumber1, May 19, 2015 IP
  9. Adrian Robertson

    Adrian Robertson Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    This is a bit misleading. Part of the point of using asynchronous GA code is to prevent the problem you describe here. Asynchronous GA code allows the rest of the page to be read and rendered by the browser, even if it fails on the GA code.

    The only "issue" with having your GA code in the head is you might still record hits, even if the user hasn't seen all of the page yet before navigating away (assuming the head has had time to load).
     
    Adrian Robertson, May 19, 2015 IP