· 8 years ago · Apr 19, 2018, 10:46 AM
1/*
2 * Copyright (c) 2017, Yanir Calisar, Tel Aviv, Israel (ycalisar at gmail.com)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this code and associated documentation files, to deal in the code without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the code.
5 * The user assumes responsibility for determining appropriate use of the code, for consequences of its use, and for checking functionality against other reliable sources.
6 * We suggest citation in publications as with any code developement work. No warrantee is given.
7 * Please help improve the code by sending suggestions or new code back.
8 */
9
10(function() {
11
12 var STORE_COOKIES = true;
13 var STORE_LOCAL_STORAGE = true;
14
15 function topDomain() {
16 var i, h, top_level_cookie = 'top_level_domain=cookie', hostname = document.location.hostname
17 .split('.');
18 for (i = hostname.length - 1; i >= 0; i--) {
19 h = hostname.slice(i).join('.');
20 document.cookie = top_level_cookie + ';domain=.' + h + ';';
21 if (document.cookie.indexOf(top_level_cookie) > -1) {
22 document.cookie = top_level_cookie.split('=')[0] + '=;domain=.'
23 + h + ';expires=Thu, 01 Jan 1970 00:00:01 GMT;';
24 return h;
25 }
26 }
27 }
28
29 var utmCookie = {
30 cookieNamePrefix : "__lt_",
31 cookieCumulativePrefix: "__cu_",
32 cookieNameFirstTouchPrefix : "__ft_",
33
34 utmParams : [ "utm_source", "utm_medium", "utm_campaign", "utm_term",
35 "utm_content" ],
36
37 cookieExpiryDays : 365,
38
39 // From http://www.quirksmode.org/js/cookies.html
40 createCookie : function(name, value, days) {
41
42 if(STORE_COOKIES) {
43 if (days) {
44 var date = new Date();
45 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
46 var expires = "; expires=" + date.toGMTString();
47 } else
48 var expires = "";
49 document.cookie = this.cookieNamePrefix + name + "=" + value
50 + expires + ";domain=." + topDomain() + "; path=/";
51 }
52 if(STORE_LOCAL_STORAGE) this.createLS(name, value);
53 },
54
55 createLS: function(name, value){
56 localStorage[this.cookieNamePrefix+ name] = value;
57 },
58
59 readCookie : function(name) {
60
61 if(STORE_COOKIES) {
62 var nameEQ = this.cookieNamePrefix + name + "=";
63 var ca = document.cookie.split(';');
64 for (var i = 0; i < ca.length; i++) {
65 var c = ca[i];
66 while (c.charAt(0) == ' ')
67 c = c.substring(1, c.length);
68 if (c.indexOf(nameEQ) == 0)
69 return c.substring(nameEQ.length, c.length);
70 }
71 if(STORE_LOCAL_STORAGE) this.readLS(name);
72 else return null;
73 }
74 if(STORE_LOCAL_STORAGE) this.readLS(name);
75 },
76
77 readLS: function(name){
78 var val = localStorage[this.cookieNamePrefix + name];
79 if(val == undefined) return null;
80 else return val;
81 },
82
83 checkIfFirstTouch : function() {
84 if(STORE_LOCAL_STORAGE) {
85 var nameEQ = this.cookieNameFirstTouchPrefix + "utm_source=";
86 var ca = document.cookie.split(';');
87 for (var i = 0; i < ca.length; i++) {
88 var c = ca[i];
89 while (c.charAt(0) == ' ')
90 c = c.substring(1, c.length);
91 if (c.indexOf(nameEQ) == 0)
92 return c.substring(nameEQ.length, c.length);
93 }
94 if(STORE_LOCAL_STORAGE) this.readLS(this.cookieNameFirstTouchPrefix + "utm_source");
95 else return null;
96 }
97 if(STORE_LOCAL_STORAGE) this.readLS(this.cookieNameFirstTouchPrefix + "utm_source");
98 },
99
100 eraseCookie : function(name) {
101 if(STORE_COOKIES) this.createCookie(name, "", -1);
102 if(STORE_LOCAL_STORAGE) delete localStorage[name];
103 },
104
105 getParameterByName : function(name) {
106 name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
107 var regexS = "[\\?&]" + name + "=([^&#]*)";
108 var regex = new RegExp(regexS);
109 var results = regex.exec(window.location.search);
110 if (results == null) {
111 return "";
112 } else {
113 return decodeURIComponent(results[1].replace(/\+/g, " "));
114 }
115 },
116
117 utmPresentInUrl : function() {
118 var present = false;
119 for (var i = 0; i < this.utmParams.length; i++) {
120 var param = this.utmParams[i];
121 var value = this.getParameterByName(param);
122 if (value != "" && value != undefined) {
123 present = true;
124 }
125 }
126 return present;
127 },
128
129 writeUtmCookieFromParams : function() {
130 for (var i = 0; i < this.utmParams.length; i++) {
131 var param = this.utmParams[i];
132 var value = this.getParameterByName(param);
133 if(STORE_COOKIES) this.createCookie(param, value, this.cookieExpiryDays);
134 if(STORE_LOCAL_STORAGE) localStorage[param] = value;
135 }
136 },
137
138 writeCookieOnce : function(name, value) {
139
140 if (STORE_COOKIES && !this.readCookie(name)) {
141 this.createCookie(name, value, this.cookieExpiryDays);
142 }
143 if(STORE_LOCAL_STORAGE && this.readLS(name) == null){
144 this.createLS(name,value);
145 }
146 },
147
148 writeReferrerOnce : function() {
149 var value = document.referrer;
150 if (value === "" || value === undefined) {
151 this.writeCookieOnce("referrer", "direct");
152 } else {
153 this.writeCookieOnce("referrer", value);
154 }
155 },
156
157 referrer : function() {
158 return this.readCookie("referrer");
159 }
160 };
161
162 if (!utmCookie.checkIfFirstTouch()) {
163 utmCookie.cookieNamePrefix = utmCookie.cookieNameFirstTouchPrefix;
164 utmCookie.writeReferrerOnce();
165
166 if (utmCookie.utmPresentInUrl()) {
167 utmCookie.writeUtmCookieFromParams();
168 }
169 utmCookie.cookieNamePrefix = "__lt_";
170 }
171
172 utmCookie.writeReferrerOnce();
173
174 if (utmCookie.utmPresentInUrl()) {
175 utmCookie.writeUtmCookieFromParams();
176
177 }
178})();