Archive for the ‘ Javascript ’ Category

Google Analytics Multiple Tracking Code

Here is a cool trick I have implemented across the EWEA web platform:

<script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-xxxxxxxx-yy']);
	_gaq.push(["_setDomainName", "none"]);
	_gaq.push(["_setAllowLinker", true]);
	_gaq.push(["_trackPageview"]);
 _gaq.push(['_setAccount', 'UA- xxxxxxxx-zz'']);
	_gaq.push(["_setDomainName", "none"]);
	_gaq.push(["_setAllowLinker", true]);
	_gaq.push(["_trackPageview"]);
 (function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
</script>

This goes at the bottom of the tags and what it does is this:

• The “yy” code is for the local site, i.e. our individual event site or our blog.
• The “zz” code is common on all the sites. This gives me a global view over all the sites as to the most popular content across all platforms.

Javascript get GET variables from URL

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Usage

Example URL: http://www.example.com/index.php?id=1&image=awesome.jpg

Calling getQueryVariable(“id”) – would return “1”.
Calling getQueryVariable(“image”) – would return “awesome.jpg”.

Jump to top of iframe

When you want to have the page jump to the top of an iframe upon clicking “Next” or “Submit”, etc, you can easily use this code:

<iframe width="620" scrolling="no" height="2500" frameborder="0" style="background-color: transparent; overflow: hidden;" allowtransparency="true" src="http://www.website.com" onload="window.parent.parent.scrollTo(0,0);"></iframe>