Frage

UPDATE http://jsfiddle.net/musicisair/rsKtp/embedded/result/


Google Analytics setzt 4 Cookies, die zu dieser Domäne bei allen Anfragen gesendet wird (und ofset Subdomains). Von dem, was ich tatsächlich nutzt sie keinen Server anweisen kann direkt ; sie sind nur mit __utm.gif als Abfrage param.

gesendet

Nun, natürlich Google Analytics liest, schreibt und wirkt auf ihre Werte, und sie werden an den GA-Tracking-Skript verfügbar sein müssen.

Also, was ich frage mich, wenn es möglich ist, zu:

  • die __utm* Cookies auf einen lokalen Speicher neu zu schreiben, nachdem ga.js sie geschrieben hat,
  • löschen sie nach ga.js ausgeführt wurde
  • umschreiben die Cookies aus dem lokalen Speicher zurück Recht auf Cookie Form vor ga.js sie liest
  • anfangen

oder Affe Patch ga.js lokale Speicher zu verwenden, bevor es das Cookie beginnt Lese- / Schreibteil.

Natürlich, wenn wir so weit aus dem Weg gehen, um die __utm* Cookies zu entfernen, wir wollen auch die Async Variante von Analytics verwenden.

Ich vermute, die nach unten Abstimmung war, weil ich nicht eine Frage stellen hat. DOH!

Meine Fragen sind:
Kann es oben?
wie beschrieben durchgeführt werden Wenn ja, warum es noch nicht geschehen ist?


Ich habe eine Standard HTML/CSS/JS Textvorlage, die YSlow, Page Speed ??geht, und Chrome Audit mit nahezu perfekten Noten. Ich bin wirklich nach einer Möglichkeit, die noch verbleibenden Cookie Bytes von Google Analytics in Browsern zu quetschen, die lokalen Speicher unterstützen.

War es hilfreich?

Lösung

Verwenden Sie diese:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

if(window.localStorage) {
    ga('create', 'UA-98765432-1', 'www.example.com', {
      'storage': 'none'
      , 'clientId': window.localStorage.getItem('ga_clientId')
    });
    ga(function(tracker) {
      window.localStorage.setItem('ga_clientId', tracker.get('clientId'));
    });
}
else {
    ga('create', 'UA-98765432-1', 'www.example.com');
}
ga('send', 'pageview');

Zuerst habe ich überprüfen, ob localStorage unterstützt wird. Wenn es unterstützt wird, dann wird die 'storage': 'none' Option Cookies deaktivieren. Nun können wir die clientId von localstorage gesetzt. Wenn es leer ist, wird Google Analytics ein neues für uns erzeugen. Wir speichern die neue (oder bestehende) clientid in localstorage nach den Tracker Lasten.

Wenn localStorage nicht unterstützt wird, benutze ich nur die reguläre Methode Analytik. Nach der Initialisierung sende ich eine SeiteAlle über ga('send', 'pageView').

Auch diese zupfen check out: http://plnkr.co/MwH6xwGK00u3CFOTzepK

Andere Tipps

Some experimentation in chrome shows that it may be possible to use getters and setters to patch document.cookie for this, something like:

document.__defineGetter__('cookie', function () {
    // Replace this with code to read from localstorage
    return "hello";
});
document.__defineSetter__('cookie', function (value) {
    // Replace this with code to save to localstorage
    console.log(value);
});

ga.js (or any other javascript) could run and access cookies as normal, they just would never get passed to the server.

Obviously this will only work in some browsers. Browsers in which it doesn't work will have to fall back to normal cookies.

There's some related ideas in this question: Is it possible to mock document.cookie in JavaScript?

Yes it can be done. You only have to request the __utm.gif with the parameters. The rest of the data is just used for keeping track of the source, session start time and/or previous visits.

You can easily transfer the cookies both ways, so your first approach should work fine.

If your second approach works... not sure. I don't know the ga.js code good enough to estimate wheter that would or would not be easily possible.

There is also a third option, run your own version of ga.js. You are not required to use the Google version.

Can it be done as described above? Yes

Why hasn't it been done?

  1. the cookies are small, there isn't that much benefit if you use cookieless domains for all your static content
  2. it's less convenient since a lot of browsers don't support it yet
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top