/
traffic-source 1.1.js
164 lines (143 loc) · 7.53 KB
/
traffic-source 1.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(function(cookieName, domain){
var traffic_source_COOKIE_TOKEN_SEPARATOR = ">>"; //separating between concatenated lead source
var traffic_source_date_SEPARATOR = "|>";
var NONE = "(none)";
domain = domain || window.location.hostname;
function getCookie(cookieName){
var name = cookieName + "=";
var cookieArray = document.cookie.split(';'); //break cookie into array
for(var i = 0; i < cookieArray.length; i++){
var cookie = cookieArray[i].replace(/^\s+|\s+$/g, ''); //replace all space with '' = delete it
if (cookie.indexOf(name)==0){
return cookie.substring(name.length,cookie.length); //
}
}
return null;
}
/*
* Takes the current time of the session, and optional addition to the traffic source.
* The output will be of the format: >>{traffic source}|>DD-MM-YY HH:MM AM/PM>>
*
*/
/*
* Uncomment currTime() in order to activate. You should also uncomment the lines in the code that call this function (all marked with a comment). If you do so, uncomment the line above them which does not employ currTime()
function currTime() {
var date = new Date();
var currDate = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var month = date.getMonth() + 1;
var year = date.getFullYear();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' makes '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = traffic_source_date_SEPARATOR + currDate + '-' + month + '-' + year + ' ' + hours + ':' + minutes + ' ' + ampm + ')';
return strTime;
}
*/
/*
Checks whether a certain parameter exist in the current browser URL. If it does, it returns its name.
It will receive "src" later in the main function
*/
function getURLParameter(param){
var pageURL = window.location.search.substring(1); //get the query string parameters without the "?"
var URLVariables = pageURL.split('&'); //break the parameters and values attached together to an array
for (var i = 0; i < URLVariables.length; i++) {
var parameterName = URLVariables[i].split('='); //break the parameters from the values
if (parameterName[0] == param) {
return parameterName[1];
}
}
return null;
}
/*
Gets the first (latest) token from a cookie: THIS__ONE>>NOT__THIS__ONE>>AND__NOT__THIS__ONE
*/
function getFirstTokenFromCookie(cookie){
var result = "";
var firstSeparatorIndex = cookie.indexOf(traffic_source_COOKIE_TOKEN_SEPARATOR);
result = firstSeparatorIndex !== -1 ? cookie.substring(0, firstSeparatorIndex) : cookie; //if there is a separator, provide the newest value no the cookie
return result;
}
/*
Set the cookie if it doesn't exist.
*/
function setCookie(cookie, value){
var expires = new Date();
expires.setTime(expires.getTime() + 62208000000); //1000*60*60*24*30*24 (2 years)
document.cookie = cookie + "=" + value + "; expires=" + expires.toGMTString() + "; domain=" + domain + "; path=/";
}
/*
Boolean, whether or not it's not NULL or Empty
*/
function isNotNullOrEmpty(string){
return string !== null && string !== "";
}
/*
Remove the protocol for the referral token
*/
function removeProtocol(href) {
return href.replace(/.*?:\/\//g, "");
}
/*
Find lead source by checking cookie, url param "src" or referrer(that order) and set new cookie traffic_source.
Do this only if cookie do not exist. This value will be used for contact form.
If cookie exist add url "src" param on the beginning of the cookie if it exists and it is not already at the beginning of the cookie.
This way "tokens" are made in traffic_source cookie. Token separator is ">>" (see above traffic_source_COOKIE_TOKEN_SEPARATOR)
*/
var urlParamSRC = getURLParameter("src"); //take the SRC value
if(document.cookie.indexOf(cookieName) === -1) { // if there is no cookie traffic_source set yet, check if there is an existing UTMZ campaign name.
var traffic_source = ""; //reset lead source value
var utmzCookie = getCookie("__utmz"); //get GA cookie
var cookieCampaignName = ""; //reset lead source value
if(utmzCookie != null) {
var UTMSRC = "utmccn=";
var start = utmzCookie.indexOf(UTMSRC);
var end = utmzCookie.indexOf("|", start);
if(start > -1) {
if(end === -1) {
end = utmzCookie.length;
}
cookieCampaignName = "utmz:" + utmzCookie.substring((start + UTMSRC.length), end); //get the value of the UTMZ, without the parameter name
}
}
if(cookieCampaignName != "" && isNotNullOrEmpty(urlParamSRC)){ //if there is a campaign name AND there is SRC value
traffic_source = urlParamSRC + traffic_source_COOKIE_TOKEN_SEPARATOR + cookieCampaignName; //concateane the SRC with the existing campaign name
// traffic_source = urlParamSRC + currTime() + traffic_source_COOKIE_TOKEN_SEPARATOR + cookieCampaignName; // Optional: add the session time
} else if(cookieCampaignName != ""){ //if there is campaign name
traffic_source = cookieCampaignName; //just use it for the new cookie
} else if(urlParamSRC != null) { //if there is not campaign name but we have SRC
traffic_source = decodeURI(urlParamSRC); // decode URI of the SRC parameter
// traffic_source = decodeURI(urlParamSRC) + currTime(); // Optional: add the session time
} else if(document.referrer != ""){ //if there is not campaign, and no src, check if there is a referrer,
var referrerHostName = removeProtocol(document.referrer);
var GOOGLE = /www.google/;
var YAHOO = /search.yahoo/;
var BING = /www.bing/;
if(GOOGLE.test(referrerHostName)){
traffic_source = "Google Search";
//traffic_source = "Google Search" + currTime(); // + Optional: Add the current session time. Choose one of them.
} else if(YAHOO.test(referrerHostName)){
traffic_source = "Yahoo Search";
//traffic_source = "Yahoo Search" + currTime();
} else if(BING.test(referrerHostName)){
traffic_source = "Bing Search";
//traffic_source = "Bing Search" + currTime();
} else {
traffic_source = referrerHostName;
//traffic_source = referrerHostName + currTime();
}
} else {
traffic_source = NONE;
}
setCookie(cookieName, traffic_source); //set the cookie
} else if(isNotNullOrEmpty(urlParamSRC)){ //if there is a cookie, add the SRC to the cookie, unless the same src already exists as the first token.
var firstToken = getFirstTokenFromCookie(getCookie(cookieName));
if(firstToken !== "" && urlParamSRC !== firstToken) {
var newTrafficSourceCookie = urlParamSRC + traffic_source_COOKIE_TOKEN_SEPARATOR + getCookie(cookieName); //concetenate new SRC with old cookie values
// var newTrafficSourceCookie = urlParamSRC + currTime() + traffic_source_COOKIE_TOKEN_SEPARATOR + getCookie(cookieName); // Optional: add the time of current session
setCookie(cookieName, newTrafficSourceCookie);
}
}
})("traffic_source", ".YOURDOMAIN.COM");