• Jump to Main Content [m]
  • Jump to Footer [f]
  • List of all Access Keys [k]

mYm

  • Artists
  • Small Business
  • Non-Profit
  • Independent
  • Video
  • About
  • Contact
  • Elizabeth Watt
  • Lynn Chandhok
  • Doug Bost
  • Lisa Pines
  • Patterson Graham
  • Sean Justice
  • Adrian Hibbs
  • WineFlirt
  • Brooklyn-Real
  • myPOS
  • Cartema
  • AtlanticTV
  • Weidenbaum
  • Rediscovering Dorothy
  • Chelsea Dayschool
  • East River Institute
  • Dyslexia Association
  • Alison Gootee
  • Berit Anderson
  • Jenna Zabala
  • MonaInk
  • Tesla Protection
  • NYCTV
  • GuerillaFX
  • MoyerCine
  • Cayman NetNews
  • 0260
  • Adam Edits
  • DirecTouch
  • CareerJournal
  • Ford Motor Company
  • StudioOne Networks
  • Business Machines
  • Team
  • Testimonials
  • More Projects
  • Design FlipBook!
  • En Español
  • Green
  • Give
  • Map
  • Directions
  • Tell A Friend
  • More Projects
  • Design FlipBook!

GAAdd: Automatic Google Analytics Code

GAAdd was a javascript function that automatically added the original google analytics code to your pages using DOM methods. Unfortunately the new google analytics code doesn't seem to work with it. Below is another method which uses PHP and our attempt at making the new code work.

  • What happened to GAAdd
  • The PHP Include
  • Where We Left Off

What happened to GAAdd

When Google came out with it's updated version of the google analytics code we tried adding it with DOM methods and were having a timing issue with the loading of the page that kept the code from working. Instead of spending a lot of time fixing it we've decided to instead just use a PHP include to put the code into our pages, which we'll talk about in a second.

The main advantage of the old GAAdd javascript function over just adding the code yourself to the pages (or even with a server-side include) is that if javascript wasn't active the code wouldn't be in your pages. Personally I think that any code that relies on javascript should be added with javascript.

The PHP Include

The easy way to include the code into all your pages is with a php include (if you're using .php pages). You'd just make a separate file (I put mine in an includes/ directory) and call it gaadd.php

<?php
//gaadd.php
 $GAAccount = "UA-000000-0"; //change this to your google account number
?>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("<?php echo $GAAccount; ?>");
pageTracker._initData();
pageTracker._trackPageview();
</script>

<!--mimoymima.com/resources/070929_insert-analytics.php-->

Then at the bottom of all your pages you'd put this line just before the </body> tag:

<?php include("includes/gaadd.php"); ?>

Where We Left Off With the Javascript

In case anyone with better DOM scripting skills than me (I'd say I'm past beginner, but not yet intermediate) is interested in fixing this script please let me know where the problem is, I'd still be interested in using it, but it's a bit beyond me at the moment.

I was adding it to every page through a global.js file which had Simon Willison's page load event calling the function. I'm pretty sure the problem lies in the line that uses document.write to write the third script tag, since they are not getting written into the page it's probably something to do with the fact that I'm adding this stuff after the DOM is completely loaded? Not sure really.

// GAAdd v.2.0 ~ Adds Google Analytics Code automatically when js is available ~ by Brent Lagerman
function GAAdd() {
	var GAAccount = 'UA-000000-0'; //change this to your google account number :: this if the code for four (test)
	//insert the first script tag
	var InsertGC1 = document.body.lastChild;
	var GoogCode1 = document.createElement('script');
	GoogCode1.type = 'text/javascript';
	InsertGC1.parentNode.insertBefore(GoogCode1, InsertGC1.nextSibling); //same as insertAfter
	GoogCode1.appendChild(document.createTextNode('var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");\n'));
	GoogCode1.appendChild(document.createTextNode('document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));'));
	//insert the second script tag
	var InsertGC2 = document.body.lastChild;
	var GoogCode2 = document.createElement('script');
	GoogCode2.type = 'text/javascript';
	GoogCode2.appendChild(document.createTextNode('var pageTracker = _gat._getTracker("'+GAAccount+'");\n'));
	GoogCode2.appendChild(document.createTextNode('pageTracker._initData();\n'));
	GoogCode2.appendChild(document.createTextNode('pageTracker._trackPageview();'));
	InsertGC2.parentNode.insertBefore(GoogCode2, InsertGC2.nextSibling);
}

To keep up with our web development articles subscribe to the mym-dev RSS feed

mYm Dev Feed RSS

« back

Bookmark This Page

news jobs resources sitemap en español stock photos client area help!
RSS Files

© 2008

mimoYmima.com
mimo brent@mimoymima.com
+1 (646) 496-3279
mima teresa@mimoymima.com
+1 (917) 821-4489
1109 Prospect Ave #3A
Brooklyn , NY , 11218