Send analytic data to different domain without res

2019-09-09 21:46发布

问题:

Precondition

  1. I own mysite.com

  2. I do not own othersite.com, but I can embed javascript code there


Question

  1. How to send analytic data from othersite.com to mysite.com ?

    • Expected : othersite.com client -> mysite.com server

    • Not expected : othersite.com client -> othersite.com server -> mysite.com server

    • Its principle seems like to be similar with Google Analytics, but I don't know the exact principle

    • I know that it couldn't be done by ajax due to cross-domain problem

  2. How does it change if I own othersite.com ?

  3. How to send analytic data without response ?

    • For example, Heap Analytics send analytic data without response

回答1:

The default scenarion with Google Analytics (and all other Web Analytics Tools I know) is to transfer data across domains by dynamically creating an image with a source that points to the tracking server and appending user data (like unique id per user) as url parameters to the image source.

Apart from everything you send via the image source you will also get the data from the http request (ip adress, user agent etc).

For a simple system you could create a script that stores the url and http data directly to a database before it returns a (1 pixel transparent) image. If you want something scalable you would probably write the data to a log file and use some currently hyped big data technology (hadoop, hive etc) for processing.

Decoupling data collection and processing is a good idea in any case, in that allows you to more easily switch components of your tracking application for improved versions without affecting the other parts of the system.

Sending an image is reliable inasfar as it works for any browser without enabling any special configurations (cors etc). It is however rather easily blocked (users just have to block pixel images or redirect calls to your server via their host file).

If the other domain was yours you could

  • track via ajax
  • read the server logs directly or pipe them to a dashboard of your choice

If you do not have access physically to the server but the owners let you configure their name servers you could run all incoming http requests through your tracking script before redirecting them to the requested page.

I took a look at Heap Analytics. They send an image request just like the other tools:

https://heapanalytics.com/h?a=236035469&u=4184751431615606&v=2274541888&s=3701858993&b=web&z=2&h=%2F&d=heapanalytics.com&t=Heap%20%7C%20Mobile%20and%20Web%20Analytics&r=https%3A%2F%2Fwww.google.de%2F&k=Screen%20Dimensions&k=1050%20x%201680&k=Window%20Width&k=1973&k=Window%20Height&k=1039&tm=1432884624859

which returns http 200 response code and an 1 pixel transparent image, so it does not look like they "track without response" after all.