attachEvent with trackLink


#1

Help!!

Having problems with getting trackLink to fire with code below :

try {
var piwikTracker = Piwik.getTracker(pkBaseURL + “piwik.php”, 3);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();

	try
	{
		var elements = document.getElementsByTagName("a");
		var regLocal1 = new RegExp("Document/Get/",i);
		var regLocal2 = new RegExp(".wmv",i);
		var regLocal3 = new RegExp(".pdf",i);
		var regLocal4 = new RegExp(".doc",i);
		 
		var elLnk;

		for(var i=0; i<elements.length; i++){
			
			
			elLnk = elements[i];
			
			//Documents
			if(regLocal1.test(elLnk.href) || regLocal3.test(elLnk.href) || regLocal4.test(elLnk.href))
			{
				elLnk.attachEvent('onclick',function() {
					
					try
					{
						//alert("here Docs");
						//var piwikTracker3 = Piwik.getTracker(pkBaseURL + "piwik.php", 3);
						piwikTracker.trackLink('TEST DOWNLOAD', 'download');
					}
					catch(err){alert("error in loading download document links events");}

				});

				
			}
			
			//Video
			if(regLocal2.test(elLnk.href))
			{
				
								
				elLnk.attachEvent('onclick',function() {
					
					try
					{
						//alert("here Video");
						//var piwikTracker2 = Piwik.getTracker(pkBaseURL + "piwik.php", 3);
						piwikTracker.trackLink(elLnk.href, 'download');
					}
					catch(err){alert("error in loading video links events");}

				});
			}
			
			
		

		}
			

	}
	catch(err){alert("error in loading links events");}




} catch( err ) {}

(Matthieu Aubry) #2

Did it use to work in 1.5.1 ?

or what is your question exactly?


(vipsoft) #3

That wouldn’t work, even if Piwik wasn’t involved.

First of all,


elLnk = elements;

should be:


elLnk = elements[ i ];

Second, use addEventListener() with a fallback to attachEvent() for non-standard IE.


#4

Vipsoft,

Thanks for the reply…!! The elements array was a mistype… However, not sure what you mean by a fallback to attachEvent. Can you provide with a code example?

Thanks a million!

kfulton1906