Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

JavaScript: Prevent _gat._getTracker from causing errors

I can I prevent these two lines from causing errors?

var pageTracker = _gat._getTracker("ZW-12345678-9");
pageTracker._trackPageview();

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" >
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello World</h1>

<script type="text/javascript">

//// Rows below may be modified
var _gat = {}; 
_gat._getTracker = function() {return 1;}
/// Rows above may be modified

/// I cannot modify code after this point ///
var pageTracker = _gat._getTracker("ZW-12345678-9");
pageTracker._trackPageview();
</script>

</body>
</html>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

For example :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" >
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello World</h1>

<script type="text/javascript">

//// Rows below may be modified
	var _gat = new function() {
		this["_getTracker"] = function(s) {
			return this[s];
		}
		this["ZW-12345678-9"] = {}
		this["ZW-12345678-9"]["_trackPageview"] = function() { 
			alert("tracked");
		}
	}

/// Rows above may be modified

/// I cannot modify code after this point ///
var pageTracker = _gat._getTracker("ZW-12345678-9");
pageTracker._trackPageview();
</script>
</body>
</html>

Open in new window

Avatar of hankknight

ASKER

Thanks, only one problem: the value of this is not always ZW-12345678-9
_gat._getTracker("ZW-12345678-9");

It is different on every page and I have no control over its value.  
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So line 12 to 13, don't worry about which one you're returning, always returnt the object for "ZW-12345678-9"
Avatar of JerseyFoo
JerseyFoo

Why are you messing with Google Analytics anyway?  If you're having errors with their code, check to see if they have a new version of their snippet available.