How to set the link tracking extension to track all links except those that match one or more conditions?

Silver Contributor
Silver Contributor
The link tracking extension has an option to track all links or only those that match one or more conditions. However, if we wish to track all links _except_ those that match one or more conditions, how would that be configured? For example, we wish to use the link tracking extension to only track outbound links. Another example would be if we only wanted to track all internal links, but not downloads or outbound or a few specific internal links, how would we do that?
6 REPLIES 6

How to set the link tracking extension to track all links except those that match one or more conditions?

Employee Emeritus

Hi Johann, The Link Tracking Extension is our legacy link tracking extension (before introducing the jQuery Extensions.) The original intent was to provide instant link tracking for all links on the page back when Google Analytics didn't have this built-in. For custom link filters, we now recommend using the jQuery Extensions and the powerful selectors that jQuery provides for determining download, exit links, etc.

How to set the link tracking extension to track all links except those that match one or more conditions?

Silver Contributor
Silver Contributor
Thank Ty. Using either the Link Tracking Extension or the jQuery Extensions, how would we go about only tracking all outbound links?

How to set the link tracking extension to track all links except those that match one or more conditions?

Tealium Employee

Hi Johann, I have a jQuery extension that I created for another client. Here are the details.

- Set your jQuery selector

- Choose "mousedown" as the trigger event

- Choose "custom" as the track event

- In the textarea, here is the code I wrote:

y=this.href; setTimeout(function(){ var a=document.createElement('a'); a.href=y; z=a.hostname; if(z!=utag.data["dom.domain"] && y.indexOf("javascript:")==-1){ utag.link({ ga_category: "offsite link", ga_action: "click", ga_label: y }); } },300);

What this code does:

First, grab the link contents

Second, a setTimeout of 300 milliseconds is used to ensure the call has time to run before the page is redirected. This is mainly for Google Analytics since they require a 200 response from the server in order for the event to be tracked. 

Third, capture the domain of the link click into "z"

Fourth, compare "z" to the domain of the current page (utag.data["dom.domain"]). It also checks to ensure it's not an internal link (javascript : ).

Last, it calls our utag.link method and passes the link click details. The GA template will then automatically be called to fire off the event to GA.

To do the same for internal links, you will just change the conditional statement to check if the link domain equals that of the current domain. It's really just switching conditionals and comparing the appropriate data.

 

Let me know if this doesn't meet your needs.

How to set the link tracking extension to track all links except those that match one or more conditions?

Employee Emeritus

Here is a jQuery selector for Exit Links (assuming my domain is 'tealium.com').

Replace the tealium.com with your domain.

 

jQuery('a[href^="//"],a[href^="http"]').not('a[href*="tealium.com"]');

 

Unfortunately because I'm using the ".not" function, this has to be in a Javascript Extension (outside of the built-in jQuery Extension)

 

Any jQuery gurus out there who can rewrite this so it works in our jQuery onHandler extension in the "jQuery Selector" field? I'll do some research myself in a bit..

How to set the link tracking extension to track all links except those that match one or more conditions?

Employee Emeritus

This might work in the jQuery onHandler for exit link filter:

 

a[href^="http"]:not(a[href*="tealium.com"]),

a[href^="//"]:not(a[href*="tealium.com"])

How to set the link tracking extension to track all links except those that match one or more conditions?

Gold Contributor
Gold Contributor
Hi Ty Thank you for jQuery code. I tried it out in our site, since most of our internal links are full URLs this captures all URLs (both internal and external) so with some research i changed the query to following and it works. $("a[href*='http://']:not([href*='"+window.location.hostname+"'])")
Public