Solved

JavaScript: Prevent  _gat._getTracker from causing errors

Posted on 2011-03-14
5
472 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now