Google Analytics from PHP

13 replies
Hi,

Just wondering if anyone has successfully executed the Google Analytics code from PHP (server side). The reason I ask is because I'm not seeing a match between google and my own site stats (captured by the server). I'm guessing that if anyone visiting my sites has Javascript disabled, then the google code won't execute.

Is it possible to call the google code from the server?

Thanks in advance, Lee.
#analytics #google #php
  • Profile picture of the author ERPConsultant
    I doubt that it would work. The Google Analytics tracking code tracks activity performed by the user on the page to give you info like bounce rates, etc. which can be done on the client-side only using javascript.
    {{ DiscussionBoard.errors[1119917].message }}
    • Profile picture of the author Steve Diamond
      Lee, AFAIK it can't be done. Certainly not in PHP, and even if you had server-side JavaScript available (e.g. SpiderMonkey) I believe it wouldn't be compatible with what the GA script is doing.

      According to the latest stats I've seen, you can expect about 5% of your site visitors to have JavaScript unavailable or disabled. One reason your GA stats are off by more than that is probably that folks click away before a page finishes loading, and the GA script is near the bottom of the page, so it never executes. There may be other reasons too. I know I've stared at my browser's status bar while the message "waiting for google-analytics.com" displayed for a long time. IOW sometimes their servers don't respond or your server can't reach theirs.

      Steve
      Signature
      Mindfulness training & coaching online
      Reduce stress | Stay focused | Keep positive and balanced
      {{ DiscussionBoard.errors[1120751].message }}
  • Profile picture of the author d101
    ... since it is javascript, it works on a webpage.

    If you would want to track something within php (server side) ... you could configure a dummy page, maybe on a "hidden" sub.domain.com .... with the Google Analytics code implemented ... then do an fopen() ... and pass your tracking parameter in the call ... this way, you would get tracking data from within php into Google Analytics ... ;-)
    {{ DiscussionBoard.errors[1128679].message }}
    • Profile picture of the author Steve Diamond
      Originally Posted by d101 View Post

      ... since it is javascript, it works on a webpage.

      If you would want to track something within php (server side) ... you could configure a dummy page, maybe on a "hidden" sub.domain.com .... with the Google Analytics code implemented ... then do an fopen() ... and pass your tracking parameter in the call ... this way, you would get tracking data from within php into Google Analytics ... ;-)
      Detlev, that's a creative idea but I don't believe it will work. When you fopen() the file, the JavaScript is not going to execute. Where would it run? It needs a browser environment to run in, and PHP doesn't provide that.

      This was asked and answered in the Google Analytics Forum recently.

      Steve
      Signature
      Mindfulness training & coaching online
      Reduce stress | Stay focused | Keep positive and balanced
      {{ DiscussionBoard.errors[1128846].message }}
      • Profile picture of the author d101
        Originally Posted by Steve Diamond View Post

        ... When you fopen() the file, the JavaScript is not going to execute. Where would it run? It needs a browser environment to run in, and PHP doesn't provide that.
        Hi Steve,

        i was conceptualizing ... hmm.
        thanks for better thinking, i see your point!

        ... so what about the curl lib ... would it provide the environment ... (?)

        It is a curious problem.

        I would think, there is some way to configure an environment 'somewhere'
        and execute that ...

        the basic idea is building some kind of bridge,
        not doing it in a direct way ...

        Detlev
        {{ DiscussionBoard.errors[1129016].message }}
        • Profile picture of the author Steve Diamond
          Originally Posted by d101 View Post

          Hi Steve,

          i was conceptualizing ... hmm.
          thanks for better thinking, i see your point!

          ... so what about the curl lib ... would it provide the environment ... (?)

          It is a curious problem.

          I would think, there is some way to configure an environment 'somewhere'
          and execute that ...

          the basic idea is building some kind of bridge,
          not doing it in a direct way ...

          Detlev
          Yes, it's an interesting problem to ponder, but I really don't think there's a solution. Using cURL would have exactly the same limitations as using fopen. There's no browser.

          And if you did send the result to the site visitor's browser (in a hidden iframe, for example) it would be subject to same settings (JavaScript disabled) that caused this question to be asked in the first place.

          Google would have to provide an API accessible from the server side, and AFAIK they don't.

          Steve
          Signature
          Mindfulness training & coaching online
          Reduce stress | Stay focused | Keep positive and balanced
          {{ DiscussionBoard.errors[1130746].message }}
          • Profile picture of the author d101
            Steve,

            I think the flaw in our thinking is,
            that we would actually need Javascript - and we do not.

            I would think, we would be able to emulate whatever the javascript in the the users browser would do aka what the _gat._getTracker() function does. when "calling home", passing all parameters. the code in ga.js or embedded files should have the answer.

            Once we found it, we could either proceed with fopen() or curl depending on the method used by the original ga.js call to actually pass the data over the net.

            Wether it would fit the Google Analytics TOS, is another question ...

            Detlev
            {{ DiscussionBoard.errors[1131641].message }}
            • Profile picture of the author Steve Diamond
              Hi, Detlev. Try it if you think so.

              I believe you'll find that http://www.google-analytics.com/ga.js is constructing all the critical calls that cause GA to store the tracking data, and those calls are themselves in JavaScript.

              I agree that it's theoretically possible that if you could construct and request the right URLs, you might be able to replicate on the server side what their script does on the client. But I wonder if you'd just run into cross-site scripting restrictions that would make the whole exercise fail anyway. Plus, their script is collecting and storing some data that I believe just aren't available on the server side. At best, you'd have gaps.

              Steve
              Signature
              Mindfulness training & coaching online
              Reduce stress | Stay focused | Keep positive and balanced
              {{ DiscussionBoard.errors[1132106].message }}
              • Profile picture of the author d101
                ... well, i just thought, if Lee needs this, there would be a way to do it.

                Personally, I prefer to let Google Analytics fetch whatever it can
                and have a real time tracking system running that gives me the data i need.

                Generally, i read 'GA stats' more like trends than like 'absolutes' and use its information to complete the picture.

                best,
                Detlev
                {{ DiscussionBoard.errors[1132771].message }}
              • Profile picture of the author d101
                Originally Posted by Steve Diamond View Post

                ...But I wonder if you'd just run into cross-site scripting restrictions that would make the whole exercise fail anyway....
                Steve,

                ... isn´t the whole concept cross-site by nature ... ;-)

                As you pointed out earlier, the javascript lives in a `bubble` in the visitors browser ... whatever it does to communicate with the GA Tracking Server can be emulated.

                If it is worth having it, it can be done.

                Detlev

                P.S: As long as the Google Analytics Terms of Service would allow it.
                {{ DiscussionBoard.errors[1132828].message }}
                • Profile picture of the author Steve Diamond
                  Originally Posted by d101 View Post

                  Steve,

                  ... isn´t the whole concept cross-site by nature ... ;-)
                  Yes, you're right. But I wonder whether Google can't protect its own scripts that execute on its servers (the ones called by http://www.google-analytics.com/ga.js) by prohibiting them from executing unless they're called from the proper context. I'm not sure about this. You'd have to try it to find out.

                  Steve
                  Signature
                  Mindfulness training & coaching online
                  Reduce stress | Stay focused | Keep positive and balanced
                  {{ DiscussionBoard.errors[1133309].message }}
  • Profile picture of the author warrich
    google analytics is a javacript code provided by google to track websites uses in many manners page view page visit geo location coutry browser city etc..
    {{ DiscussionBoard.errors[1207646].message }}
  • {{ DiscussionBoard.errors[1215480].message }}

Trending Topics