Google Analytics mette le ali
Giovedì 3 Dicembre 2009 - 08:33
di Cesare Lamanna

Problemi di rallentamento nel caricamento delle pagine a causa delle righe di Javascript usate per monitorare il sito con Google Analytics? Il team di sviluppo del servizio di Big G ha appena reso disponibile un nuovo snippet di codice che carica lo script in modalità asincrona, migliorando così le performance generali della pagina. Eccolo:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([’_setAccount’, ‘UA-XXXXX-X’]);
_gaq.push([’_trackPageview’]);
(function() {
var ga = document.createElement(’script’);
ga.src = (’https:’ == document.location.protocol ? ‘https://ssl’ :
’http://www’) + ‘.google-analytics.com/ga.js’;
ga.setAttribute(’async’, ‘true’);
document.documentElement.firstChild.appendChild(ga);
})();
</script>
Il nuovo codice per il monitoraggio (attualmente in beta) può essere usato sin da ora per sostituire quello che si usa attualmente nelle proprie pagine, collocandolo in fondo alla sezione head del documento.
Google ha approntato alcune pagine di documentazione, come questa panoramica che contiene pure le istruzioni di installazione e questa guida alla migrazione del codice con esempi.
Categoria: Scripting | Permalink
Commenti
1
Forse è meglio ricordare che al posto di UA-XXXXX-X va messo il proprio codice…
2
vabbè allora anche che
“view plaincopy to clipboard print ?”
non va copiato…ciao
:p
3
sul mio account Analytics vedo ancora il vecchio codice… dove posso trovarlo?
# - postato da Michele - 03 Dicembre 2009 - 11:06
4
Sui miei siti non noto rallentamenti significativi. Mi sa che attenderò la fine della beta
E soprattutto… lo script mettetelo in fondo alla pagina se non volete aver problemi. Meglio una view in meno nelle statistiche che un utente in meno perché si è rotto di aspettare il caricamento ;)
5
che senso ha metterlo in fondo? il nuovo script è asincrono PROPRIO per essere messo IN CIMA alla pagina e non rallentare :)
# - postato da Marco Cilia - 03 Dicembre 2009 - 12:22
6
Asincrono? Certo, lo sarebbe davvero se l’attributo async (HTML 5) fosse supportato dai browser. Ma al momento è solo per Firefox 3.6.
Personalmente non avrei molta fretta a sostituire il vecchio codice di Analytics.# - postato da Daniele De Nobili - 03 Dicembre 2009 - 13:51
7
Non è l’attributo async a rendere il caricamento dello script asincrono, ma l’appenderlo al documento con Javascript:
document.documentElement.firstChild.appendChild(ga);In effetti non sono sicura che l’attributo async serva a qualcosa :P
8
Qualche dettaglio in più qui: http://googlecode.blogspot.com.....onous.html
The second half of the snippet provides the logic that loads the tracking code in parallel with other scripts on the page. It executes an anonymous function that dynamically creates a element and sets the source with the proper protocol. As a result, most browsers will load the tracking code in parallel with other scripts on the page, thus reducing the web page load time. Note here the forward-looking use of the new HTML5 “async” attribute in this part of the snippet. While it creates the same effect as adding a <script> element to the DOM, it officially tells browsers that this script can be loaded asynchronously. Firefox 3.6 is the first browser to officially offer support for this new feature. If you’re curious, here are more details on the official HTML5 async specification.
9
Grazie per la precisazione: non avevo idea che “creando” l’elemento anziché scriverlo con document.write, si potesse avere un caricamento asincrono. Approfondirò di sicuro questa tecnica!
# - postato da Daniele De Nobili - 04 Dicembre 2009 - 10:48
10
Saranno anche piu’ veloci … ma fanno perdere un sacco di tempo ;-)
# - postato da sLa - 05 Dicembre 2009 - 01:07







