nasty
nasty

Reputation: 7077

Show different Google analytics javascripts based on url parameter

I want to display different Google analytics codes based on a URL parameter

I have different affiliate IDs attached in the url and these affiliate IDs get carried across the site as users browser through. Whats the best way to show different scripts based on ID?

Sample Affiliate id

http://www.website.com.au/?afid=AFF1

http://www.website.com.au/?afid=AFF2

This is what I have right now

var CurrentUrl = window.location.href;
var SplitUrl = CurrentUrl.split('afid=')[1];

if (SplitUrl === 'AFF1') {  
 //show GA code one

}

else if (SplitUrl === 'AFF2') {
  //show GA code two
}

Upvotes: 0

Views: 34

Answers (1)

Anthony Veach
Anthony Veach

Reputation: 320

You can dynamically create the ga tracker:

ga('create', 'UA-XXXX-Y', 'auto'); (https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced)

You just have to include the GA js before that point.

Upvotes: 1

Related Questions