2

To give a quick example of my question, consider the JS file provided by Google to capture Analytics data that we paste in our files. How does this JS file get all the data? I want to know how to create such JS files that I can give out to others who can then run it on their own websites and yet, store data on my website server. In short, they only copy the 1-2 lines of JS in their pages and all the intended functionality would run on their website, but the data will be stored on my web server database.

How can I attach the functionality to such a JS file? I mean, how can I tell the JS file whether to collect Analytic data or show a form, etc. I am using PHP for server side processing. I did not find any good information on this so far.

Edit 1:

I wanted to add that the functionality is NOT limited just to analytics. It could be even as simple as showing a contact form that sends email to recipients, etc.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Devner
  • 6,825
  • 11
  • 63
  • 104

3 Answers3

2

Google Analytics has a client-side javascript file that the site-owner puts a reference to in their web page. When that javascript file runs, it collects information about the current page and then makes a request of Google's server with that information encoded in the request and Google's server records that information in their database. Because ajax calls are subject to the same-origin limitations, Google's request back to their server is actually for a GIF image with the data encoded in the URL.

Here's Google's explanation of how it works: http://code.google.com/apis/analytics/docs/concepts/gaConceptsOverview.html

To create something like this for your clients, you would have to create the appropriate javascript file, host it on your servers, give out the instructions for installing it into their web pages and create the right PHP scripts for recording the information that comes in when the GIF is requested (presumably, you'd have to do some web server configuration to get your PHP scripts to run on a GIF request too).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks for the pointers. I can build a website based on PHP & JS, but I have no clue on how to deploy only JS file and even execute PHP using the same and hence my question. – Devner Mar 04 '12 at 20:41
1

I think you can find the answer to your question here: How to send data to remote server using Javascript

In short, you'll be able to send data to another domain using JSONP. You can achieve this also with jQuery's, $.getJson method.

Community
  • 1
  • 1
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
  • 1
    You can use JSONP, but since a response from the server is not required, JSONP is not required. Google Analytics just requests a GIF image with the data encoded in the URL. – jfriend00 Mar 04 '12 at 19:01
  • Thanks for this as it looks promising. I will be taking a look at this in depth. – Devner Mar 04 '12 at 20:42
0

By inserting something like

<script src="http://myeviltrackingsite.com/track.js"></script>

the visitor's browser will ask your server for track.js. When asked your server will get a normal HTTP-Header from the visitor and of course his IP. This HTTP-Header contains all information you want like the visitor's language, the kind of browser he uses. To track the visitor's geo location you can use the visitor's IP address and do a reverse IP lookup. there are free geo location databases available.

Basti
  • 3,998
  • 1
  • 18
  • 21
  • Thanks for the response but I have no clue on how to do this and hence my question. User geo tracking is just one example of what can be done apart from other actions. I am trying to get to know more about how to do this on technical basis. So please post if you have any technical info on this. – Devner Mar 04 '12 at 20:44
  • If you don't understand this, start with the basics of PHP. There is no magic in parsing request headers or getting the visitors IP. – Basti Mar 05 '12 at 06:16