SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can i move my google analytics to my windows.onload function?

    So this is what I've done so far:

    <head>
    ...
    <script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script>
    <script type="text/javascript">
    _uacct = "UA-<uaid>";
    window.onload=function()
    {
    Nifty(...); // rounded edges calls that i want done before i do my sometimes slow tracking js calls

    // tracking calls
    urchinTracker();
    var mybloglogurl = "http://track2.mybloglog.com/js/jsserv.php?mblID=<mblid>";
    var mybloglogscript = document.createElement("script");
    mybloglogscript.setAttribute("type", "text/javascript");
    mybloglogscript.setAttribute("src", mybloglogurl);
    document.body.appendChild(mybloglogscript);
    }
    </script>
    </head>

    However, I would like to move the google analytics urchin.js to load in my windows.onload function. However, when I move it there and do the document.body.append thing, it doesn't look like it blocks until the JS is loaded. So if I try to call the urchinTracker() function immediately after, I get a JS error. Any thoughts on how I can load google analytics and then call the function in my windows.onload?

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...I'm not sure if I follow your reasons for doing this, but if this is simply to get around the occasional performance lags when requesting urchin.js, might I suggest you keep your urchin.js in the foot and then run a local copy on your server?

    Putting urchin.js back in the foot will give a mild bump in the perceived performance since the page can load before the javascript. Running it locally will eliminate the lag when their server is under a load.

    You'll need to check for an updated urchin.js once daily, but that beats every page load by a country mile. How-To Forge has a pretty decent tutorial, or a quick google search will turn up a couple of other approaches.

    Kid Koala


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
  •