Giter VIP home page Giter VIP logo

js-history-tracker's Introduction

js-history-tracker

Usage A site simply needs to include a script tag that points to the tracker.js file. e.g.

<script src="//wuddrum.github.io/js-history-tracker/tracker.js"></script>

Implementation The implementation relies on two files tracker.js and tracker.html, these should always be next to each other.

Tracker.js: Referenced by a site in a simple script tag. Inserts an invisible iframe element in the site that executes the tracking code. When the iframe is loaded, it passes current window href to the iframe. Tracker.html: Executed inside an iframe that's created by tracker.js. The iframe is needed in order for the tracking script to access and store cookies on the tracker domain. The data is stored in the default cookies, so that it can be easily cleared by users.

The tracking script checks if the domain name is already added to the history cookie, if not, then it adds a new history entry with the domain and current datetime in UTC. Currently the cookie expire date is set to one year.

Afterwards the pixel gif is called with domain history from cookies and current url. Currently the tracking pixel url looks like this: //example.com/pixel.gif?data={}&i={}. Where data parameter contains an encoded pixel data JSON object and i parameter is simply current date ticks, to ensure that the tracking pixel is retrieved from your site, instead of user's cache. If you prefer for the data to be used as the pixel filename, then it can be easily changed.

The implementation is compatible with all modern browsers and IE9+. It was working fine even on an old iPhone 3Gs.

Data Structures The cookie history is saved as an encoded JSON object. This is the object's structure:

{
    "domain1.com": "Thu, 11 Apr 2019 15:25:41 GMT",
    "domain2.com": "Thu, 11 Apr 2019 15:25:41 GMT"
}

As you can see, it's a simple key-value pair of domain and its datetime, when first loaded. The domain names are not guaranteed to be in order, so you'll need to sort them by dates on the backend to reveal the chronological user's browsing habits.

The pixel data is sent as an encoded JSON object. This is the object's structure:

{
    "originHref": "http://domain2.com/item/13/",
    "history": {
        "domain1.com": "Thu, 11 Apr 2019 15:25:41 GMT",
        "domain2.com": "Thu, 11 Apr 2019 15:25:41 GMT"
    }
}

Which is simply history data object concatenated with current page url.

Alternative I think the more logical alternative to all this would be to handle the cookie creation/tracking on the backend.

A site would do a simple tracking pixel request and the backend could inspect the attached cookies (if any) and construct/update new ones that get returned to the client along with the 200 response. The domain/page url could be retrieved from the Referer parameter that's sent by the client.

The big downside of this of course is that the tracking wouldn't be capable of fully working if the Referer header is not sent by a client (site meta property, removed by firewall, proxy etc.). But it might be better if it didn't work in these cases, since it honors the privacy level chosen by site or user.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.