Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript on link click w/ Google Analytics 1

Status
Not open for further replies.

gtjr921

Programmer
Aug 30, 2006
115
Ok I am new to javascript, saying I know almost nothing about to would be accurate.
My site is set up with the javascript from google so I can track web traffic to my site.
I have this code on all my pages to monitor my site.
Except I want to know how often the mp3 files on my site are clicked.
I have some links on my website that open up MP3 and also pdf files
(they are legal mp3 that i did, not that it matters)
I want to know how i can create a client click event (or whatever event i need) that will send that information to my google account and have that count as a page view, so i can monitor the traffic on those files
Or if there is some way I can send the data to a text file that would work too.
I prefer the method that will send the info to my google analytics account.

Thanks for the help
 
first of all, i'm not familiar with the google script your using, if you can post it i can have a look at it. also you can try using an <iframe> in your page.

but if your could post the script your using i can tell you exactly what to do.
 
i found this bit of code from google
Code:
<script src="[URL unfurl="true"]http://www.google-analytics.com/urchin.js"[/URL] type="text/javascript">
</script>
<script type="text/javascript">
  _uacct="UA-xxxx-x";
  urchinTracker();
</script>

if this is what you are using, you can have a link like so in theory.

Code:
<a ONCLICK="urchinTracker();" href="myFile.mp3">click for sound</a>

but this will not fully work, because you need to tell urchinTracker() what URL of the file your going to view.
 
yes that is the google code i am using.
so is there anyway to get it to fully work?
 
there IS a way, but i'm still looking for it, it shouldn't be too hard.
 
also, if your server supports a PHP or PERL script, that would be easy to do, only you would have separate statistics. also you should be able to track a file with a redirect.

it might look like this...

main HTML page
Code:
<a ONCLICK="window.location='myMP3.html';" href="#">click for sound</a>

myMP3.html page
Code:
<script src="[URL unfurl="true"]http://www.google-analytics.com/urchin.js"[/URL] type="text/javascript">
</script>
<script type="text/javascript">
  _uacct="UA-xxxx-x";
  urchinTracker();
</script> 


then use a redirect to your myMP3.mp3 file
<script>
window.location='myMP3.mp3';
</script>

this is long, and more work for a lot of pages, but it should work, but i'm still looking at the google script.
 
Ok I don't think I can use perl or php i don't have any control over the server. It's a hosting service and i don't think they allow perl or php, but i could try using a redirect with an aspx page. Like you said that is the long way.
Plus there are a Lot of pages I would have to make to do that.
If you can come up with a better solution that would be great
I appreciate your willingness to help

Thanks
 
Ok i think i found the key...

Code:
 if ((_userv==1 || _userv==2) && _uSP()) {
  var i2=new Image(1,1);
  i2.src=_ugifpath2+"?"+"utmwv="+_uwv+s+"&utmac="+_uacct+"&utmcc="+_uGCS();
  i2.onload=function() { _uVoid(); }
 }
 return;

i was trying to figure out how the page sent the information to the google server and its right at the end of function _uInfo();

i2 is an "image" it "loads" an image from the google server but it sends the information out though the URL of the image. those google guys are smart.

so the line ends up being this...

Google Info Send
Code:
[URL unfurl="true"]http://www.google-analytics.com/__utm.gif?utmwv=1&utmn=136211636&utmcs=windows-1252&utmsr=1280x1024&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=9.0&utmhn=setnb.com&utmr=-&utmp=/&utmac=UA-2004967-1&utmcc=__utma%3D265485709.729596747.1175045298.1175049778.1181089302.3%3B%2B__utmb%3D265485709%3B%2B__utmc%3D265485709%3B%2B__utmz%3D265485709.1175045298.1.1.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B[/URL]

so you could have a link like so
Code:
var _MyMp3File = ^^ URL from above ^^;
var MyMp3Redir = "myMp3.mp3";
<a href="#" onclick="var i2=new Image(1,1);i2.src=_MyMp3File;i2.onload=function(void(0););window.location=MyMp3Redir;">MP3.mp3</a>

You would have to change the info in the Google Info Send to your own domain and acount, but that should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top