Solved

JavaScript: Prevent  _gat._getTracker from causing errors

Posted on 2011-03-14
5
476 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:hankknight
  • 3
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35133053
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

0
 
LVL 16

Author Comment

by:hankknight
ID: 35133126
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.  
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35133307
So line 12 to 13, don't worry about which one you're returning, always returnt the value for "ZW-12345678-9" :


this["_getTracker"] = function(s) {
			return this["ZW-12345678-9"];
		}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35133312
So line 12 to 13, don't worry about which one you're returning, always returnt the object for "ZW-12345678-9"
0
 
LVL 1

Expert Comment

by:JerseyFoo
ID: 35133540
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - why isn't this ng-if isn't working? 4 36
Javascript closure 1 35
JS Plugin Chaining 2 32
Having problem getting this to work 2 19
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now