SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to include google analytics code the right way?

    Whenever we include the code given by google analytics in the static web page, it does not validate...
    Is their any way it can be put in an external javascript file & called from <head>

  2. #2
    SitePoint Addict Poiesis01's Avatar
    Join Date
    Jun 2007
    Location
    Cape Town
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I remember correctly, it has to be placed just before the closing </ body> tag.

  3. #3
    SitePoint Enthusiast hartofalyon's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mine is also right above the </body> tag and it validates.

  4. #4
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a validation problem with analytics code that was puzzling. I had copied the code exactly as google provided it, and yet the validation problem was occurring.

    Finally, I wondered whether it could be the line-break problem in Notepad, to which I was copying the google code and then copying it from there to my webpage code. So I changed the Wordwrap option off in Notepad, copied the code from google once again, and then created an shtml file that was placed in the page template just above the closing </body> tag.

    And the problem disappeared.
    Think like a Marketer and become a Marketing Consultant

  5. #5
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm Yes.. It validates, but,

    Isn't there a better, intelligent options if we can keep the code as external js file? That way it'll load in cache

  6. #6
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you notice that an "shtml" file was mentioned in my post?

    an shtml file is an external file that is called by any html page that wants it using the #include call.

    Just like an external js file, shtml also need to be written only once, and once uploaded, it can be called by the regular pages.
    Think like a Marketer and become a Marketing Consultant

  7. #7
    SitePoint Addict Romuba's Avatar
    Join Date
    Jun 2007
    Location
    Cape Town, South Africa
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by businesswriter View Post
    Did you notice that an "shtml" file was mentioned in my post?
    Initially I also used SSI (shtml) but then discovered php and also that most hosting companies have php but fewer have support for SSI.

    I have therefore placed my Google Analytics code into a seperate html file that I call via a php include between the </head> and <body> tags.

    I use these includes for anything that will be common to all or many of my pages.
    Ross Bartholomew
    Web Designer/Developer
    BartWebSites
    E-mail: ross@bartwebsites.com

  8. #8
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by businesswriter View Post
    Did you notice that an "shtml" file was mentioned in my post?

    an shtml file is an external file that is called by any html page that wants it using the #include call.

    Just like an external js file, shtml also need to be written only once, and once uploaded, it can be called by the regular pages.
    I checked your code, but it uses the <script> code just above the body..

    there's no shtml code.. Or am I looking somewhere else?

  9. #9
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Romuba View Post
    Initially I also used SSI (shtml) but then discovered php and also that most hosting companies have php but fewer have support for SSI.

    I have therefore placed my Google Analytics code into a seperate html file that I call via a php include between the </head> and <body> tags.

    I use these includes for anything that will be common to all or many of my pages.
    Hmmm, how to do this?? can you please show any site where you used this or give an example of the code?

  10. #10
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    the thing is that you need to print into the client's browser code given by google in any way you can.
    You can put it into static html page or statically put into .php script and
    can include from php also.

    .....
    ....
    <?php
    include_once("code.php"); // this will print anything inside code.php
    ?>
    </body>
    </html>

  11. #11
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by santha View Post
    I checked your code, but it uses the <script> code just above the body..

    there's no shtml code.. Or am I looking somewhere else?
    First you create a file named, say, 'analytics-tracker.shtml' with the tracker code:
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>

    You upload the above .shtml file into your server.

    Next you call it from your pages' html code as below:
    <head>
    ...
    </head>

    <body>
    <div class="main">
    <div class="leftcol">
    ...
    </div>
    </div><!--#include file="analytics-tracker.shtml" -->
    </body>
    </html>

    You upload the pages to the server as they are created.

    If you view the source code of the pages, you won't find the #include call. Instead you will find the tracker code expanded and included at the relevant place, as below:
    ... </div><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>
    </body>
    </html>
    Think like a Marketer and become a Marketing Consultant

  12. #12
    SitePoint Addict Romuba's Avatar
    Join Date
    Jun 2007
    Location
    Cape Town, South Africa
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by businesswriter View Post
    ...
    </head>

    <body>
    <div class="main">
    <div class="leftcol">
    ...
    </div>
    </div><!--#include file="analytics-tracker.shtml" -->
    </body>
    </html>[/INDENT]
    All correct but Google says that the actual code must be placed between the </head> and the <body> tags and not in the actual body section as noted above.
    Ross Bartholomew
    Web Designer/Developer
    BartWebSites
    E-mail: ross@bartwebsites.com

  13. #13
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Romuba View Post
    All correct but Google says that the actual code must be placed between the </head> and the <body> tags and not in the actual body section as noted above.
    1. If I remember correctly, what Google says is to insert the code just before the ending </body> tag. I'll check again.
    2. The code I had given were all taken from my website with only the ID dotted out. It has been been working for long
    Think like a Marketer and become a Marketing Consultant

  14. #14
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Romuba View Post
    Initially I also used SSI (shtml) but then discovered php and also that most hosting companies have php but fewer have support for SSI.

    I have therefore placed my Google Analytics code into a seperate html file that I call via a php include between the </head> and <body> tags.

    I use these includes for anything that will be common to all or many of my pages.
    This is true. So persons comfortable with PHP should use that option.

    Also, some webhosts do not automatically include the expanded shtml code in the html file. They might require some directives to be included in the .htaccess file before .shtml files are processed.
    Think like a Marketer and become a Marketing Consultant

  15. #15
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skinstc View Post
    Hi guys,

    the thing is that you need to print into the client's browser code given by google in any way you can.
    You can put it into static html page or statically put into .php script and
    can include from php also.

    .....
    ....
    <?php
    include_once("code.php"); // this will print anything inside code.php
    ?>
    </body>
    </html>
    I'm not a php dev., but i think it soes like this:

    1. First create a .php file, ex: analytics.php and put the google's js code into it:

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>


    2. call the analytics.php file just above </body>

    <?php
    include_once("analytics.php");
    ?>


    Is this right?

    The question is: Will this page ex: index.html will change into index.php? or .html will work ?

  16. #16
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Romuba View Post
    All correct but Google says that the actual code must be placed between the </head> and the <body> tags and not in the actual body section as noted above.
    Google asks to put the code "just above </body>"

  17. #17
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Romuba View Post
    All correct but Google says that the actual code must be placed between the </head> and the <body> tags and not in the actual body section as noted above.
    Nothing may appear between </head> and <body> in an HTML document except whitespace.

    http://www.w3.org/TR/html401/struct/global.html

  18. #18
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by santha View Post
    I'm not a php dev., but i think it soes like this:

    1. First create a .php file, ex: analytics.php and put the google's js code into it:

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>


    2. call the analytics.php file just above </body>

    <?php
    include_once("analytics.php");
    ?>


    Is this right?

    The question is: Will this page ex: index.html will change into index.php? or .html will work ?
    hello, can you answer that? Anybody here?

  19. #19
    SitePoint Addict Romuba's Avatar
    Join Date
    Jun 2007
    Location
    Cape Town, South Africa
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Nothing may appear between </head> and <body> in an HTML document except whitespace.

    http://www.w3.org/TR/html401/struct/global.html
    My apologies, I have checked again and see that it must be immediately before the </body> tag and not immediately before the <body> as I thought - I now need to change the location of all my google scripts. Interesting because they have been working fine between the </head> and <body> tags!?!?
    Ross Bartholomew
    Web Designer/Developer
    BartWebSites
    E-mail: ross@bartwebsites.com

  20. #20
    SitePoint Member
    Join Date
    Mar 2009
    Location
    India
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by santha View Post
    hello, can you answer that? Anybody here?
    You might have better luck if you try at the PHP forum.
    Think like a Marketer and become a Marketing Consultant

  21. #21
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you are wondering if you can put a php include() into a .html file? If so, the answer is no, you have to rename your index file from index.html to index.php. In fact, any page that you need to include your include() google analytics code needs to have a .php extension.

    so:

    index.php
    PHP Code:
    <html>
    <head>
    </head>

    <body>
    ** CONTENT **

    <?php include('google.html'); ?>
    </body>
    </html>
    google.html
    Code HTML4Strict:
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>

    hope that helps.. try renaming one of your server files to .php and see if it works (ie: contact.html to contact.php). If so, just rename all your extensions to .php and do a mass find and replace on your website for .html and replace it with .php. Be sure to copy all your files and try to do a replace on a copy of the website.

  22. #22
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The calling page (index) needs to be parsed by PHP in order for the include to work.
    HTML has no ability to access files on the server like that.

    Presumably your site is more than one page, so you'll need to change everything to PHP in order to be able to reference a single instance of your analytics code.

    That creates another problem, which is that if all your .html files become .php you could break a lot of links and loose a lot of search engine position.
    You'll either need to contact your host and ask if they can configure PHP to process html pages (this is possible), which will negate the need to change a lot of extensions, or
    you'll need to use an Apache Mod Rewrite rule so that requests for .html docs are actually processed by the equivalent .php page.
    Complex eh?

    Code:
    # example .htaccess file (Apache configuration)
    RewriteEngine On
    RewriteRule (.+)\.html$ $1.php
    With this .htaccess file you still rename all .html files to .php (no links break).
    When I request example.html the page will still load (hooray) even though example.html is now actually example.php
    PHP will process the page and your GA include will work.

  23. #23
    SitePoint Zealot santha's Avatar
    Join Date
    Jan 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ripcurlksm View Post
    It sounds like you are wondering if you can put a php include() into a .html file? If so, the answer is no, you have to rename your index file from index.html to index.php. In fact, any page that you need to include your include() google analytics code needs to have a .php extension.

    so:

    index.php
    PHP Code:
    <html>
    <head>
    </head>

    <body>
    ** CONTENT **

    <?php include('google.html'); ?>
    </body>
    </html>
    google.html
    Code HTML4Strict:
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>

    hope that helps.. try renaming one of your server files to .php and see if it works (ie: contact.html to contact.php). If so, just rename all your extensions to .php and do a mass find and replace on your website for .html and replace it with .php. Be sure to copy all your files and try to do a replace on a copy of the website.
    Thanks! can't name page to .php as there are many incoming links.

  24. #24
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Santhi,

    Once got the Google analysis code, you can put the code before closing [</body>] body tag. like past the code like:
    <script src="#" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "...";
    urchinTracker();
    </script>

  25. #25
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes u can rename them, use mod rewrite to redirect those incoming links to a php extension instead! do it!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •