Still a work in progress, not ready for usage.
Tries to mim Classic Google Analytics methodology to calculate traffic source client side.
On Universal Analytics all this is calculated server side, so it's unavailable in case you need it client side.
| function getParam(s, q) { | |
| try{ | |
| return s.match(RegExp('(^|&)'+q+'=([^&]*)'))[2]; | |
| } catch(e){ | |
| return ''; | |
| } | |
| } | |
| function calculateTrafficSource() { | |
| var source='', medium='', campaign='', term='', content=''; | |
| var search_engines = [['bing', 'q'], ['google', 'q'], ['yahoo', 'q']]; | |
| var ref = document.referrer; | |
| ref = ref.substr(ref.indexOf('//')+2); | |
| ref_domain = ref; | |
| ref_path = '/'; | |
| ref_search = ''; | |
| // Checks for campaign parameters | |
| var url_search = document.location.search; | |
| if(url_search.indexOf('utm_source') > -1) { | |
| source = getParam(url_search, 'utm_source'); | |
| medium = getParam(url_search, 'utm_medium'); | |
| campaign = getParam(url_search, 'utm_campaign'); | |
| term = getParam(url_search, 'utm_term'); | |
| content = getParam(url_search, 'utm_content'); | |
| } else if (getParam(url_search, 'gclid')) { | |
| source = 'google'; | |
| medium = 'cpc'; | |
| } else if(ref) { | |
| // separate domain, path and query parameters | |
| if (ref.indexOf('/') > -1) { | |
| ref_domain = ref.substr(0,ref.indexOf('/')); | |
| ref_path = ref.substr(ref.indexOf('/')); | |
| if (ref_path.indexOf('?') > -1) { | |
| ref_search = ref_path.substr(ref_path.indexOf('?')+1); | |
| ref_path = ref_path.substr(0, ref_path.indexOf('?')); | |
| } | |
| } | |
| medium = 'referral'; | |
| source = ref_domain; | |
| // Extract term for organic source | |
| for (var i=0; i<search_engines.length; i++){ | |
| if(ref_domain.indexOf(search_engines[i][0]) > -1){ | |
| medium = 'organic'; | |
| source = search_engines[i][0]; | |
| term = getParam(ref_search, search_engines[i][1]) || '(not provided)'; | |
| break; | |
| } | |
| } | |
| } | |
| return { | |
| 'source' : source, | |
| 'medium' : medium, | |
| 'campaign': campaign, | |
| 'term' : term, | |
| 'content' : content | |
| }; | |
| } | |
| function getTrafficSource(cookieName, hostname) { | |
| var trafficSources = calculateTrafficSource(); | |
| var value = 'source=' + trafficSources.source + | |
| '&medium=' + trafficSources.medium + | |
| '&campaign='+ trafficSources.campaign + | |
| '&term=' + trafficSources.term + | |
| '&content=' + trafficSources.content; | |
| if(trafficSources.source && trafficSources.source.indexOf(hostname) === -1) { | |
| // Use new value if not self-referral | |
| value = value; | |
| } else if(getParam(cookieName, document.cookie)) { | |
| // Use Previous value | |
| value = getParam(cookieName, document.cookie); | |
| } else { | |
| value = 'source=(none)&medium=direct'; | |
| } | |
| // Set cookie | |
| var days = 30 * 6; // 6 months | |
| var date = new Date(); | |
| date.setTime(date.getTime()+(days*24*60*60*1000)); | |
| var expires = "; expires="+date.toGMTString(); | |
| document.cookie = cookieName+"="+value+expires+"; path=/; domain=" + hostname; | |
| } |
Hello, I see you didn't modify this anymore, what does it need to be ready for usage?