user3195757
user3195757

Reputation: 1

Communicating between client and server

If I were to build an app where a user could put javascript code on their website and, for example, it would track the number of impressions the site got, what would be the best way to send the information to my server?

Upvotes: 0

Views: 67

Answers (3)

sissonb
sissonb

Reputation: 3780

I would recommend using a web bug/pixel tracking. This makes a GET request to the server. This request wont be blocked by the same origin policy. The big analytics companies all use web bugs. Omniture and Google Analytics.

Basically you do a fake image request and put some extra tracking data on the call.

var webBug = new Image();
webBug.src = "http://yourserver.com/tracking/?visitorId=abc123&sessionId=123abc"

or just put the image into the html,

<img src="http://yourserver.com/tracking/?visitorId=abc123&sessionId=123abc"/>

http://en.wikipedia.org/wiki/Web_bug

Upvotes: 0

Thomas
Thomas

Reputation: 572

Here is a small function to send an XMLHttpRequest:

function sendXMLRequest()
{
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET","http://myserver.com", true);
  xmlhttp.send();
}

(adapted from http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp)

You would also have to find some way of calling this function when the site gets an 'impression' (or whatever event you want to track).

Upvotes: 0

Andrew Templeton
Andrew Templeton

Reputation: 1696

XHR / XMLHttpRequest

The verb is probably best implemented as POST here.

MDN API link is the best I can give since it's a vague scenario:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Upvotes: 1

Related Questions