Go Back   WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk
Register Blogs FAQ Social Groups CalendarHelp Desk

Reply
 
LinkBack Thread Tools
Old 08-26-2009, 04:11 AM   #1
The Architect
War Room Member
 
lharding's Avatar
 
Join Date: Jun 2009
Location: Cambridgeshire, UK
Posts: 113
Thanks: 18
Thanked 17 Times in 17 Posts
Contact Info
Send a message via Skype™ to lharding
Default Google Analytics from PHP

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.

Lee Harding
The Architect
lharding is offline   Reply With Quote
Old 08-26-2009, 07:34 AM   #2
Active Warrior
War Room Member
 
Join Date: Jun 2009
Posts: 68
Thanks: 10
Thanked 12 Times in 9 Posts
Default Re: Google Analytics from PHP

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.
ERPConsultant is offline   Reply With Quote
Old 08-26-2009, 11:43 AM   #3
Senior Warrior Member
War Room Member
 
Steve Diamond's Avatar
 
Join Date: Apr 2006
Location: Tucson, AZ, USA.
Posts: 1,025
Thanks: 120
Thanked 158 Times in 115 Posts
Social Networking View Member's FaceBook Profile  View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Skype™ to Steve Diamond
Default Re: Google Analytics from PHP

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

Executive I.T. consulting for small/medium business
Website development | PHP - MySQL - JavaScript expert programming
Software requirements analysis | Specification writing
Project management | Vendor relationship management
Steve Diamond is offline   Reply With Quote
Old 08-28-2009, 05:04 PM   #4
Detlev Eller
War Room Member
 
d101's Avatar
 
Join Date: Aug 2009
Location: Republic of San Marino (RSM)
Posts: 41
Thanks: 22
Thanked 2 Times in 2 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Lightbulb Re: Google Analytics from PHP

... 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 ... ;-)
d101 is offline   Reply With Quote
Old 08-28-2009, 06:10 PM   #5
Senior Warrior Member
War Room Member
 
Steve Diamond's Avatar
 
Join Date: Apr 2006
Location: Tucson, AZ, USA.
Posts: 1,025
Thanks: 120
Thanked 158 Times in 115 Posts
Social Networking View Member's FaceBook Profile  View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Skype™ to Steve Diamond
Default Re: Google Analytics from PHP

Quote:
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

Executive I.T. consulting for small/medium business
Website development | PHP - MySQL - JavaScript expert programming
Software requirements analysis | Specification writing
Project management | Vendor relationship management
Steve Diamond is offline   Reply With Quote
Old 08-28-2009, 07:20 PM   #6
Detlev Eller
War Room Member
 
d101's Avatar
 
Join Date: Aug 2009
Location: Republic of San Marino (RSM)
Posts: 41
Thanks: 22
Thanked 2 Times in 2 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Default Re: Google Analytics from PHP

Quote:
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
d101 is offline   Reply With Quote
Old 08-29-2009, 10:57 AM   #7
Senior Warrior Member
War Room Member
 
Steve Diamond's Avatar
 
Join Date: Apr 2006
Location: Tucson, AZ, USA.
Posts: 1,025
Thanks: 120
Thanked 158 Times in 115 Posts
Social Networking View Member's FaceBook Profile  View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Skype™ to Steve Diamond
Default Re: Google Analytics from PHP

Quote:
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

Executive I.T. consulting for small/medium business
Website development | PHP - MySQL - JavaScript expert programming
Software requirements analysis | Specification writing
Project management | Vendor relationship management
Steve Diamond is offline   Reply With Quote
Old 08-29-2009, 06:16 PM   #8
Detlev Eller
War Room Member
 
d101's Avatar
 
Join Date: Aug 2009
Location: Republic of San Marino (RSM)
Posts: 41
Thanks: 22
Thanked 2 Times in 2 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Arrow Re: Google Analytics from PHP

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
d101 is offline   Reply With Quote
Old 08-29-2009, 10:31 PM   #9
Senior Warrior Member
War Room Member
 
Steve Diamond's Avatar
 
Join Date: Apr 2006
Location: Tucson, AZ, USA.
Posts: 1,025
Thanks: 120
Thanked 158 Times in 115 Posts
Social Networking View Member's FaceBook Profile  View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Skype™ to Steve Diamond
Default Re: Google Analytics from PHP

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

Executive I.T. consulting for small/medium business
Website development | PHP - MySQL - JavaScript expert programming
Software requirements analysis | Specification writing
Project management | Vendor relationship management
Steve Diamond is offline   Reply With Quote
Old 08-30-2009, 06:23 AM   #10
Detlev Eller
War Room Member
 
d101's Avatar
 
Join Date: Aug 2009
Location: Republic of San Marino (RSM)
Posts: 41
Thanks: 22
Thanked 2 Times in 2 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Default Re: Google Analytics from PHP

... 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

d101 is offline   Reply With Quote
Old 08-30-2009, 07:09 AM   #11
Detlev Eller
War Room Member
 
d101's Avatar
 
Join Date: Aug 2009
Location: Republic of San Marino (RSM)
Posts: 41
Thanks: 22
Thanked 2 Times in 2 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Default Re: Google Analytics from PHP

Quote:
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.

d101 is offline   Reply With Quote
Old 08-30-2009, 11:30 AM   #12
Senior Warrior Member
War Room Member
 
Steve Diamond's Avatar
 
Join Date: Apr 2006
Location: Tucson, AZ, USA.
Posts: 1,025
Thanks: 120
Thanked 158 Times in 115 Posts
Social Networking View Member's FaceBook Profile  View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Skype™ to Steve Diamond
Default Re: Google Analytics from PHP

Quote:
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

Executive I.T. consulting for small/medium business
Website development | PHP - MySQL - JavaScript expert programming
Software requirements analysis | Specification writing
Project management | Vendor relationship management
Steve Diamond is offline   Reply With Quote
Old 09-22-2009, 02:32 PM   #13
Advanced Warrior
War Room Member
 
warrich's Avatar
 
Join Date: Sep 2009
Posts: 653
Thanks: 2
Thanked 27 Times in 24 Posts
Social Networking View Member's Twitter Profile  View Member's YouTube Profile
Contact Info
Send a message via Yahoo to warrich Send a message via Skype™ to warrich
Default Re: Google Analytics from PHP

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..

warrich is offline   Reply With Quote
Old 09-24-2009, 06:39 PM   #14
Lisa Dozois
War Room Member
 
lisag's Avatar
 
Join Date: Jan 2006
Location: Florida, USA.
Posts: 612
Thanks: 85
Thanked 221 Times in 110 Posts
Social Networking View Member's Twitter Profile 
Default Re: Google Analytics from PHP

Google Analytics without javascript! link love

-- Lisa G
lisag is offline   Reply With Quote
Reply

  WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk

Tags
analytics, google, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -6. The time now is 03:31 PM.