├── .gitignore ├── LICENSE.txt ├── README.md ├── dest ├── utm_form-1.0.3.js ├── utm_form-1.0.3.min.js ├── utm_form-1.0.4.js ├── utm_form-1.0.4.min.js ├── utm_form-1.1.0.js ├── utm_form-1.1.0.min.js ├── utm_form-1.2.0.js └── utm_form-1.2.0.min.js ├── gulpfile.js ├── lib ├── utm_cookie.coffee └── utm_form.coffee ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Puru Choudhary 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What does it do? 2 | It adds UTM, referrer, landing page and other information to any lead generation form. It adds this extra information 3 | as hidden fields on a form. 4 | 5 | Here's a blog post discussing it in detail: http://bit.ly/1z0r3dN 6 | 7 | ## Why do I need it? 8 | If you want to know where each email list subscriber or lead is coming from, use this script to help with that. This 9 | is different from analytics tools where you know this information in aggregate. 10 | 11 | e.g. You'll know that bob@example.com originally came from Twitter, landed on page www.yoursite.com/promotion and 12 | visited your website 3 times before giving you his email address. 13 | 14 | **Information it adds to your forms:** 15 | * 5 UTM parameters - Any UTM parameters in the URL that a visitor used to come to your website will be added to the form 16 | * 5 Initial UTM parameters (optional) - The UTM parameters in the very first URL that the visitor used 17 | * Initial Referrer - The webpage where the visitor came from for the first time 18 | * Last Referrer - The webpage where the visitor came from most recently 19 | * Initial Landing page - URL of the page on your website where the visitor landed the very first time 20 | * Number of visits - The number of times the visitor came to your website before filling your form 21 | * Additional parameters defined in `additional_params_map` 22 | * Additional initial parameters defined in `additional_initial_params_map` 23 | 24 | ## How do I use it? 25 | 26 | 27 | 1. Just add this before the closing `` tag at the bottom of your page. Make sure to change the domain. 28 | 29 | ```html 30 | 34 | 35 | 36 | ``` 37 | 38 | There might be newer versions available in `dest` folder that are not yet available via CDN. Feel free to download the latest files in that case. 39 | 40 | 2. You need to make your forms accept the new fields. Based on the information available for a visitor, the fields added 41 | to your form will be, 42 | 43 | * USOURCE - Value of *utm_source* if present 44 | * UMEDIUM - Value of *utm_medium* if present 45 | * UCAMPAIGN - Value of *utm_campaign* if present 46 | * UCONTENT - Value of *utm_content* if present 47 | * UTERM - Value of *utm_term* if present 48 | * IREFERRER - URL of the initial referrer 49 | * LREFERRER - URL of the last referrer 50 | * ILANDPAGE - Initial landing page 51 | * VISITS - Number of visits 52 | 53 | Note: A new visit happens when the visitor comes to your website after more than an hour (can be customized as shown below). 54 | 55 | ## How will my form look like? 56 | 57 | Let's say your lead generation form looks like this before the script is added. 58 | ```html 59 |
60 | 61 | 62 | 63 |
64 | ``` 65 | 66 | Once the script is added, your form will look like this after the page is loaded. 67 | 68 | ```html 69 |
70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |
82 | ``` 83 | 84 | When someone submits the form, all the extra information is also sent along with the email address. 85 | 86 | You'll need to make sure that your form can accept these values. If it is a Mailchimp form, configure it to 87 | accept these fields. Same for ConstantContact, CampaignMonitor, Hubspot, ActiveCampaign or any other service. 88 | 89 | ## Customize your forms 90 | If you would like to customize how fields get added to your form, following options are available: 91 | 92 | ```html 93 | 129 | 130 | 131 | ``` 132 | 133 | ## More Questions? 134 | #### What happens if someone visits a bunch of pages on my website/blog before filling the form? 135 | It doesn't matter. As soon as they land on your website, the script saves the information in a cookie. This 136 | cookie is valid for 365 days. It adds this information to your form from the saved cookie. 137 | 138 | #### What's the session length for? 139 | It's used to count the number of visits. If someone comes to your website after the session has expired, it will be counted 140 | as a new visit. 141 | 142 | #### Can I look at the code? 143 | You are on Github and all the code is available above. Knock yourself out. Even better, send a pull request and we can 144 | make it better together. 145 | 146 | ## Development 147 | Utm Form is written in CoffeeScript and compiles to Javascript. 148 | 149 | ### Setup 150 | 1. Clone the repository 151 | 2. Make sure you have node and npm installed: `brew install node`. 152 | 3. Install the required packages: `npm install`. 153 | 154 | ### Compiling 155 | 156 | Run `gulp build` 157 | 158 | This will compile the CoffeeScript and also minify it. The generated files can be found in `dest` folder. 159 | -------------------------------------------------------------------------------- /dest/utm_form-1.0.3.js: -------------------------------------------------------------------------------- 1 | var UtmCookie; 2 | 3 | UtmCookie = (function() { 4 | function UtmCookie(options) { 5 | if (options == null) { 6 | options = {}; 7 | } 8 | this._cookieNamePrefix = '_uc_'; 9 | this._domain = options.domain; 10 | this._sessionLength = options.sessionLength || 1; 11 | this._cookieExpiryDays = options.cookieExpiryDays || 365; 12 | this._additionalParams = options.additionalParams || []; 13 | this._utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; 14 | this.writeInitialReferrer(); 15 | this.writeLastReferrer(); 16 | this.writeInitialLandingPageUrl(); 17 | this.setCurrentSession(); 18 | if (this.additionalParamsPresentInUrl()) { 19 | this.writeAdditionalParams(); 20 | } 21 | if (this.utmPresentInUrl()) { 22 | this.writeUtmCookieFromParams(); 23 | } 24 | return; 25 | } 26 | 27 | UtmCookie.prototype.createCookie = function(name, value, days, path, domain, secure) { 28 | var cookieDomain, cookieExpire, cookiePath, cookieSecure, date, expireDate; 29 | expireDate = null; 30 | if (days) { 31 | date = new Date; 32 | date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); 33 | expireDate = date; 34 | } 35 | cookieExpire = expireDate != null ? '; expires=' + expireDate.toGMTString() : ''; 36 | cookiePath = path != null ? '; path=' + path : '; path=/'; 37 | cookieDomain = domain != null ? '; domain=' + domain : ''; 38 | cookieSecure = secure != null ? '; secure' : ''; 39 | document.cookie = this._cookieNamePrefix + name + '=' + escape(value) + cookieExpire + cookiePath + cookieDomain + cookieSecure; 40 | }; 41 | 42 | UtmCookie.prototype.readCookie = function(name) { 43 | var c, ca, i, nameEQ; 44 | nameEQ = this._cookieNamePrefix + name + '='; 45 | ca = document.cookie.split(';'); 46 | i = 0; 47 | while (i < ca.length) { 48 | c = ca[i]; 49 | while (c.charAt(0) === ' ') { 50 | c = c.substring(1, c.length); 51 | } 52 | if (c.indexOf(nameEQ) === 0) { 53 | return c.substring(nameEQ.length, c.length); 54 | } 55 | i++; 56 | } 57 | return null; 58 | }; 59 | 60 | UtmCookie.prototype.eraseCookie = function(name) { 61 | this.createCookie(name, '', -1, null, this._domain); 62 | }; 63 | 64 | UtmCookie.prototype.getParameterByName = function(name) { 65 | var regex, regexS, results; 66 | name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); 67 | regexS = '[\\?&]' + name + '=([^&#]*)'; 68 | regex = new RegExp(regexS); 69 | results = regex.exec(window.location.search); 70 | if (results) { 71 | return decodeURIComponent(results[1].replace(/\+/g, ' ')); 72 | } else { 73 | return ''; 74 | } 75 | }; 76 | 77 | UtmCookie.prototype.additionalParamsPresentInUrl = function() { 78 | var j, len, param, ref; 79 | ref = this._additionalParams; 80 | for (j = 0, len = ref.length; j < len; j++) { 81 | param = ref[j]; 82 | if (this.getParameterByName(param)) { 83 | return true; 84 | } 85 | } 86 | return false; 87 | }; 88 | 89 | UtmCookie.prototype.utmPresentInUrl = function() { 90 | var j, len, param, ref; 91 | ref = this._utmParams; 92 | for (j = 0, len = ref.length; j < len; j++) { 93 | param = ref[j]; 94 | if (this.getParameterByName(param)) { 95 | return true; 96 | } 97 | } 98 | return false; 99 | }; 100 | 101 | UtmCookie.prototype.writeCookie = function(name, value) { 102 | this.createCookie(name, value, this._cookieExpiryDays, null, this._domain); 103 | }; 104 | 105 | UtmCookie.prototype.writeAdditionalParams = function() { 106 | var j, len, param, ref, value; 107 | ref = this._additionalParams; 108 | for (j = 0, len = ref.length; j < len; j++) { 109 | param = ref[j]; 110 | value = this.getParameterByName(param); 111 | this.writeCookie(param, value); 112 | } 113 | }; 114 | 115 | UtmCookie.prototype.writeUtmCookieFromParams = function() { 116 | var j, len, param, ref, value; 117 | ref = this._utmParams; 118 | for (j = 0, len = ref.length; j < len; j++) { 119 | param = ref[j]; 120 | value = this.getParameterByName(param); 121 | this.writeCookie(param, value); 122 | } 123 | }; 124 | 125 | UtmCookie.prototype.writeCookieOnce = function(name, value) { 126 | var existingValue; 127 | existingValue = this.readCookie(name); 128 | if (!existingValue) { 129 | this.writeCookie(name, value); 130 | } 131 | }; 132 | 133 | UtmCookie.prototype._sameDomainReferrer = function(referrer) { 134 | var hostname; 135 | hostname = document.location.hostname; 136 | return referrer.indexOf(this._domain) > -1 || referrer.indexOf(hostname) > -1; 137 | }; 138 | 139 | UtmCookie.prototype._isInvalidReferrer = function(referrer) { 140 | return referrer === '' || referrer === void 0; 141 | }; 142 | 143 | UtmCookie.prototype.writeInitialReferrer = function() { 144 | var value; 145 | value = document.referrer; 146 | if (this._isInvalidReferrer(value)) { 147 | value = 'direct'; 148 | } 149 | this.writeCookieOnce('referrer', value); 150 | }; 151 | 152 | UtmCookie.prototype.writeLastReferrer = function() { 153 | var value; 154 | value = document.referrer; 155 | if (!this._sameDomainReferrer(value)) { 156 | if (this._isInvalidReferrer(value)) { 157 | value = 'direct'; 158 | } 159 | this.writeCookie('last_referrer', value); 160 | } 161 | }; 162 | 163 | UtmCookie.prototype.writeInitialLandingPageUrl = function() { 164 | var value; 165 | value = this.cleanUrl(); 166 | if (value) { 167 | this.writeCookieOnce('initial_landing_page', value); 168 | } 169 | }; 170 | 171 | UtmCookie.prototype.initialReferrer = function() { 172 | return this.readCookie('referrer'); 173 | }; 174 | 175 | UtmCookie.prototype.lastReferrer = function() { 176 | return this.readCookie('last_referrer'); 177 | }; 178 | 179 | UtmCookie.prototype.initialLandingPageUrl = function() { 180 | return this.readCookie('initial_landing_page'); 181 | }; 182 | 183 | UtmCookie.prototype.incrementVisitCount = function() { 184 | var cookieName, existingValue, newValue; 185 | cookieName = 'visits'; 186 | existingValue = parseInt(this.readCookie(cookieName), 10); 187 | newValue = 1; 188 | if (isNaN(existingValue)) { 189 | newValue = 1; 190 | } else { 191 | newValue = existingValue + 1; 192 | } 193 | this.writeCookie(cookieName, newValue); 194 | }; 195 | 196 | UtmCookie.prototype.visits = function() { 197 | return this.readCookie('visits'); 198 | }; 199 | 200 | UtmCookie.prototype.setCurrentSession = function() { 201 | var cookieName, existingValue; 202 | cookieName = 'current_session'; 203 | existingValue = this.readCookie(cookieName); 204 | if (!existingValue) { 205 | this.createCookie(cookieName, 'true', this._sessionLength / 24, null, this._domain); 206 | this.incrementVisitCount(); 207 | } 208 | }; 209 | 210 | UtmCookie.prototype.cleanUrl = function() { 211 | var cleanSearch; 212 | cleanSearch = window.location.search.replace(/utm_[^&]+&?/g, '').replace(/&$/, '').replace(/^\?$/, ''); 213 | return window.location.origin + window.location.pathname + cleanSearch + window.location.hash; 214 | }; 215 | 216 | return UtmCookie; 217 | 218 | })(); 219 | 220 | var UtmForm, _uf; 221 | 222 | UtmForm = (function() { 223 | function UtmForm(options) { 224 | if (options == null) { 225 | options = {}; 226 | } 227 | this._utmParamsMap = {}; 228 | this._utmParamsMap.utm_source = options.utm_source_field || 'USOURCE'; 229 | this._utmParamsMap.utm_medium = options.utm_medium_field || 'UMEDIUM'; 230 | this._utmParamsMap.utm_campaign = options.utm_campaign_field || 'UCAMPAIGN'; 231 | this._utmParamsMap.utm_content = options.utm_content_field || 'UCONTENT'; 232 | this._utmParamsMap.utm_term = options.utm_term_field || 'UTERM'; 233 | this._additionalParamsMap = options.additional_params_map || {}; 234 | this._initialReferrerField = options.initial_referrer_field || 'IREFERRER'; 235 | this._lastReferrerField = options.last_referrer_field || 'LREFERRER'; 236 | this._initialLandingPageField = options.initial_landing_page_field || 'ILANDPAGE'; 237 | this._visitsField = options.visits_field || 'VISITS'; 238 | this._addToForm = options.add_to_form || 'all'; 239 | this._formQuerySelector = options.form_query_selector || 'form'; 240 | this.utmCookie = new UtmCookie({ 241 | domain: options.domain, 242 | sessionLength: options.sessionLength, 243 | cookieExpiryDays: options.cookieExpiryDays, 244 | additionalParams: Object.getOwnPropertyNames(this._additionalParamsMap) 245 | }); 246 | if (this._addToForm !== 'none') { 247 | this.addAllFields(); 248 | } 249 | } 250 | 251 | UtmForm.prototype.addAllFields = function() { 252 | var fieldName, param, ref, ref1; 253 | ref = this._utmParamsMap; 254 | for (param in ref) { 255 | fieldName = ref[param]; 256 | this.addFormElem(fieldName, this.utmCookie.readCookie(param)); 257 | } 258 | ref1 = this._additionalParamsMap; 259 | for (param in ref1) { 260 | fieldName = ref1[param]; 261 | this.addFormElem(fieldName, this.utmCookie.readCookie(param)); 262 | } 263 | this.addFormElem(this._initialReferrerField, this.utmCookie.initialReferrer()); 264 | this.addFormElem(this._lastReferrerField, this.utmCookie.lastReferrer()); 265 | this.addFormElem(this._initialLandingPageField, this.utmCookie.initialLandingPageUrl()); 266 | this.addFormElem(this._visitsField, this.utmCookie.visits()); 267 | }; 268 | 269 | UtmForm.prototype.addFormElem = function(fieldName, fieldValue) { 270 | var allForms, firstForm, form, i, len; 271 | if (fieldValue) { 272 | allForms = document.querySelectorAll(this._formQuerySelector); 273 | if (allForms.length > 0) { 274 | if (this._addToForm === 'first') { 275 | firstForm = allForms[0]; 276 | this.insertAfter(this.getFieldEl(fieldName, fieldValue), firstForm.lastChild); 277 | } else { 278 | for (i = 0, len = allForms.length; i < len; i++) { 279 | form = allForms[i]; 280 | this.insertAfter(this.getFieldEl(fieldName, fieldValue), form.lastChild); 281 | } 282 | } 283 | } 284 | } 285 | }; 286 | 287 | UtmForm.prototype.getFieldEl = function(fieldName, fieldValue) { 288 | var fieldEl; 289 | fieldEl = document.createElement('input'); 290 | fieldEl.type = "hidden"; 291 | fieldEl.name = fieldName; 292 | fieldEl.value = fieldValue; 293 | return fieldEl; 294 | }; 295 | 296 | UtmForm.prototype.insertAfter = function(newNode, referenceNode) { 297 | return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 298 | }; 299 | 300 | return UtmForm; 301 | 302 | })(); 303 | 304 | _uf = window._uf || {}; 305 | 306 | window.UtmForm = new UtmForm(_uf); 307 | -------------------------------------------------------------------------------- /dest/utm_form-1.0.3.min.js: -------------------------------------------------------------------------------- 1 | var UtmCookie;UtmCookie=function(){function UtmCookie(options){null==options&&(options={}),this._cookieNamePrefix="_uc_",this._domain=options.domain,this._sessionLength=options.sessionLength||1,this._cookieExpiryDays=options.cookieExpiryDays||365,this._additionalParams=options.additionalParams||[],this._utmParams=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.writeInitialReferrer(),this.writeLastReferrer(),this.writeInitialLandingPageUrl(),this.setCurrentSession(),this.additionalParamsPresentInUrl()&&this.writeAdditionalParams(),this.utmPresentInUrl()&&this.writeUtmCookieFromParams()}return UtmCookie.prototype.createCookie=function(name,value,days,path,domain,secure){var cookieDomain,cookieExpire,cookiePath,cookieSecure,date,expireDate;expireDate=null,days&&(date=new Date,date.setTime(date.getTime()+24*days*60*60*1e3),expireDate=date),cookieExpire=null!=expireDate?"; expires="+expireDate.toGMTString():"",cookiePath=null!=path?"; path="+path:"; path=/",cookieDomain=null!=domain?"; domain="+domain:"",cookieSecure=null!=secure?"; secure":"",document.cookie=this._cookieNamePrefix+name+"="+escape(value)+cookieExpire+cookiePath+cookieDomain+cookieSecure},UtmCookie.prototype.readCookie=function(name){var c,ca,i,nameEQ;for(nameEQ=this._cookieNamePrefix+name+"=",ca=document.cookie.split(";"),i=0;ij;j++)if(param=ref[j],this.getParameterByName(param))return!0;return!1},UtmCookie.prototype.utmPresentInUrl=function(){var j,len,param,ref;for(ref=this._utmParams,j=0,len=ref.length;len>j;j++)if(param=ref[j],this.getParameterByName(param))return!0;return!1},UtmCookie.prototype.writeCookie=function(name,value){this.createCookie(name,value,this._cookieExpiryDays,null,this._domain)},UtmCookie.prototype.writeAdditionalParams=function(){var j,len,param,ref,value;for(ref=this._additionalParams,j=0,len=ref.length;len>j;j++)param=ref[j],value=this.getParameterByName(param),this.writeCookie(param,value)},UtmCookie.prototype.writeUtmCookieFromParams=function(){var j,len,param,ref,value;for(ref=this._utmParams,j=0,len=ref.length;len>j;j++)param=ref[j],value=this.getParameterByName(param),this.writeCookie(param,value)},UtmCookie.prototype.writeCookieOnce=function(name,value){var existingValue;existingValue=this.readCookie(name),existingValue||this.writeCookie(name,value)},UtmCookie.prototype._sameDomainReferrer=function(referrer){var hostname;return hostname=document.location.hostname,referrer.indexOf(this._domain)>-1||referrer.indexOf(hostname)>-1},UtmCookie.prototype._isInvalidReferrer=function(referrer){return""===referrer||void 0===referrer},UtmCookie.prototype.writeInitialReferrer=function(){var value;value=document.referrer,this._isInvalidReferrer(value)&&(value="direct"),this.writeCookieOnce("referrer",value)},UtmCookie.prototype.writeLastReferrer=function(){var value;value=document.referrer,this._sameDomainReferrer(value)||(this._isInvalidReferrer(value)&&(value="direct"),this.writeCookie("last_referrer",value))},UtmCookie.prototype.writeInitialLandingPageUrl=function(){var value;value=this.cleanUrl(),value&&this.writeCookieOnce("initial_landing_page",value)},UtmCookie.prototype.initialReferrer=function(){return this.readCookie("referrer")},UtmCookie.prototype.lastReferrer=function(){return this.readCookie("last_referrer")},UtmCookie.prototype.initialLandingPageUrl=function(){return this.readCookie("initial_landing_page")},UtmCookie.prototype.incrementVisitCount=function(){var cookieName,existingValue,newValue;cookieName="visits",existingValue=parseInt(this.readCookie(cookieName),10),newValue=1,newValue=isNaN(existingValue)?1:existingValue+1,this.writeCookie(cookieName,newValue)},UtmCookie.prototype.visits=function(){return this.readCookie("visits")},UtmCookie.prototype.setCurrentSession=function(){var cookieName,existingValue;cookieName="current_session",existingValue=this.readCookie(cookieName),existingValue||(this.createCookie(cookieName,"true",this._sessionLength/24,null,this._domain),this.incrementVisitCount())},UtmCookie.prototype.cleanUrl=function(){var cleanSearch;return cleanSearch=window.location.search.replace(/utm_[^&]+&?/g,"").replace(/&$/,"").replace(/^\?$/,""),window.location.origin+window.location.pathname+cleanSearch+window.location.hash},UtmCookie}();var UtmForm,_uf;UtmForm=function(){function UtmForm(options){null==options&&(options={}),this._utmParamsMap={},this._utmParamsMap.utm_source=options.utm_source_field||"USOURCE",this._utmParamsMap.utm_medium=options.utm_medium_field||"UMEDIUM",this._utmParamsMap.utm_campaign=options.utm_campaign_field||"UCAMPAIGN",this._utmParamsMap.utm_content=options.utm_content_field||"UCONTENT",this._utmParamsMap.utm_term=options.utm_term_field||"UTERM",this._additionalParamsMap=options.additional_params_map||{},this._initialReferrerField=options.initial_referrer_field||"IREFERRER",this._lastReferrerField=options.last_referrer_field||"LREFERRER",this._initialLandingPageField=options.initial_landing_page_field||"ILANDPAGE",this._visitsField=options.visits_field||"VISITS",this._addToForm=options.add_to_form||"all",this._formQuerySelector=options.form_query_selector||"form",this.utmCookie=new UtmCookie({domain:options.domain,sessionLength:options.sessionLength,cookieExpiryDays:options.cookieExpiryDays,additionalParams:Object.getOwnPropertyNames(this._additionalParamsMap)}),"none"!==this._addToForm&&this.addAllFields()}return UtmForm.prototype.addAllFields=function(){var fieldName,param,ref,ref1;ref=this._utmParamsMap;for(param in ref)fieldName=ref[param],this.addFormElem(fieldName,this.utmCookie.readCookie(param));ref1=this._additionalParamsMap;for(param in ref1)fieldName=ref1[param],this.addFormElem(fieldName,this.utmCookie.readCookie(param));this.addFormElem(this._initialReferrerField,this.utmCookie.initialReferrer()),this.addFormElem(this._lastReferrerField,this.utmCookie.lastReferrer()),this.addFormElem(this._initialLandingPageField,this.utmCookie.initialLandingPageUrl()),this.addFormElem(this._visitsField,this.utmCookie.visits())},UtmForm.prototype.addFormElem=function(fieldName,fieldValue){var allForms,firstForm,form,i,len;if(fieldValue&&(allForms=document.querySelectorAll(this._formQuerySelector),allForms.length>0))if("first"===this._addToForm)firstForm=allForms[0],this.insertAfter(this.getFieldEl(fieldName,fieldValue),firstForm.lastChild);else for(i=0,len=allForms.length;len>i;i++)form=allForms[i],this.insertAfter(this.getFieldEl(fieldName,fieldValue),form.lastChild)},UtmForm.prototype.getFieldEl=function(fieldName,fieldValue){var fieldEl;return fieldEl=document.createElement("input"),fieldEl.type="hidden",fieldEl.name=fieldName,fieldEl.value=fieldValue,fieldEl},UtmForm.prototype.insertAfter=function(newNode,referenceNode){return referenceNode.parentNode.insertBefore(newNode,referenceNode.nextSibling)},UtmForm}(),_uf=window._uf||{},window.UtmForm=new UtmForm(_uf); -------------------------------------------------------------------------------- /dest/utm_form-1.0.4.js: -------------------------------------------------------------------------------- 1 | var UtmCookie; 2 | 3 | UtmCookie = (function() { 4 | function UtmCookie(options) { 5 | if (options == null) { 6 | options = {}; 7 | } 8 | this._cookieNamePrefix = '_uc_'; 9 | this._domain = options.domain; 10 | this._sessionLength = options.sessionLength || 1; 11 | this._cookieExpiryDays = options.cookieExpiryDays || 365; 12 | this._additionalParams = options.additionalParams || []; 13 | this._utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; 14 | this.writeInitialReferrer(); 15 | this.writeLastReferrer(); 16 | this.writeInitialLandingPageUrl(); 17 | this.setCurrentSession(); 18 | if (this.additionalParamsPresentInUrl()) { 19 | this.writeAdditionalParams(); 20 | } 21 | if (this.utmPresentInUrl()) { 22 | this.writeUtmCookieFromParams(); 23 | } 24 | return; 25 | } 26 | 27 | UtmCookie.prototype.createCookie = function(name, value, days, path, domain, secure) { 28 | var cookieDomain, cookieExpire, cookiePath, cookieSecure, date, expireDate; 29 | expireDate = null; 30 | if (days) { 31 | date = new Date; 32 | date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); 33 | expireDate = date; 34 | } 35 | cookieExpire = expireDate != null ? '; expires=' + expireDate.toGMTString() : ''; 36 | cookiePath = path != null ? '; path=' + path : '; path=/'; 37 | cookieDomain = domain != null ? '; domain=' + domain : ''; 38 | cookieSecure = secure != null ? '; secure' : ''; 39 | document.cookie = this._cookieNamePrefix + name + '=' + escape(value) + cookieExpire + cookiePath + cookieDomain + cookieSecure; 40 | }; 41 | 42 | UtmCookie.prototype.readCookie = function(name) { 43 | var c, ca, i, nameEQ; 44 | nameEQ = this._cookieNamePrefix + name + '='; 45 | ca = document.cookie.split(';'); 46 | i = 0; 47 | while (i < ca.length) { 48 | c = ca[i]; 49 | while (c.charAt(0) === ' ') { 50 | c = c.substring(1, c.length); 51 | } 52 | if (c.indexOf(nameEQ) === 0) { 53 | return c.substring(nameEQ.length, c.length); 54 | } 55 | i++; 56 | } 57 | return null; 58 | }; 59 | 60 | UtmCookie.prototype.eraseCookie = function(name) { 61 | this.createCookie(name, '', -1, null, this._domain); 62 | }; 63 | 64 | UtmCookie.prototype.getParameterByName = function(name) { 65 | var regex, regexS, results; 66 | name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); 67 | regexS = '[\\?&]' + name + '=([^&#]*)'; 68 | regex = new RegExp(regexS); 69 | results = regex.exec(window.location.search); 70 | if (results) { 71 | return decodeURIComponent(results[1].replace(/\+/g, ' ')); 72 | } else { 73 | return ''; 74 | } 75 | }; 76 | 77 | UtmCookie.prototype.additionalParamsPresentInUrl = function() { 78 | var j, len, param, ref; 79 | ref = this._additionalParams; 80 | for (j = 0, len = ref.length; j < len; j++) { 81 | param = ref[j]; 82 | if (this.getParameterByName(param)) { 83 | return true; 84 | } 85 | } 86 | return false; 87 | }; 88 | 89 | UtmCookie.prototype.utmPresentInUrl = function() { 90 | var j, len, param, ref; 91 | ref = this._utmParams; 92 | for (j = 0, len = ref.length; j < len; j++) { 93 | param = ref[j]; 94 | if (this.getParameterByName(param)) { 95 | return true; 96 | } 97 | } 98 | return false; 99 | }; 100 | 101 | UtmCookie.prototype.writeCookie = function(name, value) { 102 | this.createCookie(name, value, this._cookieExpiryDays, null, this._domain); 103 | }; 104 | 105 | UtmCookie.prototype.writeAdditionalParams = function() { 106 | var j, len, param, ref, value; 107 | ref = this._additionalParams; 108 | for (j = 0, len = ref.length; j < len; j++) { 109 | param = ref[j]; 110 | value = this.getParameterByName(param); 111 | this.writeCookie(param, value); 112 | } 113 | }; 114 | 115 | UtmCookie.prototype.writeUtmCookieFromParams = function() { 116 | var j, len, param, ref, value; 117 | ref = this._utmParams; 118 | for (j = 0, len = ref.length; j < len; j++) { 119 | param = ref[j]; 120 | value = this.getParameterByName(param); 121 | this.writeCookie(param, value); 122 | } 123 | }; 124 | 125 | UtmCookie.prototype.writeCookieOnce = function(name, value) { 126 | var existingValue; 127 | existingValue = this.readCookie(name); 128 | if (!existingValue) { 129 | this.writeCookie(name, value); 130 | } 131 | }; 132 | 133 | UtmCookie.prototype._sameDomainReferrer = function(referrer) { 134 | var hostname; 135 | hostname = document.location.hostname; 136 | return referrer.indexOf(this._domain) > -1 || referrer.indexOf(hostname) > -1; 137 | }; 138 | 139 | UtmCookie.prototype._isInvalidReferrer = function(referrer) { 140 | return referrer === '' || referrer === void 0; 141 | }; 142 | 143 | UtmCookie.prototype.writeInitialReferrer = function() { 144 | var value; 145 | value = document.referrer; 146 | if (this._isInvalidReferrer(value)) { 147 | value = 'direct'; 148 | } 149 | this.writeCookieOnce('referrer', value); 150 | }; 151 | 152 | UtmCookie.prototype.writeLastReferrer = function() { 153 | var value; 154 | value = document.referrer; 155 | if (!this._sameDomainReferrer(value)) { 156 | if (this._isInvalidReferrer(value)) { 157 | value = 'direct'; 158 | } 159 | this.writeCookie('last_referrer', value); 160 | } 161 | }; 162 | 163 | UtmCookie.prototype.writeInitialLandingPageUrl = function() { 164 | var value; 165 | value = this.cleanUrl(); 166 | if (value) { 167 | this.writeCookieOnce('initial_landing_page', value); 168 | } 169 | }; 170 | 171 | UtmCookie.prototype.initialReferrer = function() { 172 | return this.readCookie('referrer'); 173 | }; 174 | 175 | UtmCookie.prototype.lastReferrer = function() { 176 | return this.readCookie('last_referrer'); 177 | }; 178 | 179 | UtmCookie.prototype.initialLandingPageUrl = function() { 180 | return this.readCookie('initial_landing_page'); 181 | }; 182 | 183 | UtmCookie.prototype.incrementVisitCount = function() { 184 | var cookieName, existingValue, newValue; 185 | cookieName = 'visits'; 186 | existingValue = parseInt(this.readCookie(cookieName), 10); 187 | newValue = 1; 188 | if (isNaN(existingValue)) { 189 | newValue = 1; 190 | } else { 191 | newValue = existingValue + 1; 192 | } 193 | this.writeCookie(cookieName, newValue); 194 | }; 195 | 196 | UtmCookie.prototype.visits = function() { 197 | return this.readCookie('visits'); 198 | }; 199 | 200 | UtmCookie.prototype.setCurrentSession = function() { 201 | var cookieName, existingValue; 202 | cookieName = 'current_session'; 203 | existingValue = this.readCookie(cookieName); 204 | if (!existingValue) { 205 | this.createCookie(cookieName, 'true', this._sessionLength / 24, null, this._domain); 206 | this.incrementVisitCount(); 207 | } 208 | }; 209 | 210 | UtmCookie.prototype.cleanUrl = function() { 211 | var cleanSearch; 212 | cleanSearch = window.location.search.replace(/utm_[^&]+&?/g, '').replace(/&$/, '').replace(/^\?$/, ''); 213 | return window.location.origin + window.location.pathname + cleanSearch + window.location.hash; 214 | }; 215 | 216 | return UtmCookie; 217 | 218 | })(); 219 | 220 | var UtmForm, _uf; 221 | 222 | UtmForm = (function() { 223 | function UtmForm(options) { 224 | if (options == null) { 225 | options = {}; 226 | } 227 | this._utmParamsMap = {}; 228 | this._utmParamsMap.utm_source = options.utm_source_field || 'USOURCE'; 229 | this._utmParamsMap.utm_medium = options.utm_medium_field || 'UMEDIUM'; 230 | this._utmParamsMap.utm_campaign = options.utm_campaign_field || 'UCAMPAIGN'; 231 | this._utmParamsMap.utm_content = options.utm_content_field || 'UCONTENT'; 232 | this._utmParamsMap.utm_term = options.utm_term_field || 'UTERM'; 233 | this._additionalParamsMap = options.additional_params_map || {}; 234 | this._initialReferrerField = options.initial_referrer_field || 'IREFERRER'; 235 | this._lastReferrerField = options.last_referrer_field || 'LREFERRER'; 236 | this._initialLandingPageField = options.initial_landing_page_field || 'ILANDPAGE'; 237 | this._visitsField = options.visits_field || 'VISITS'; 238 | this._addToForm = options.add_to_form || 'all'; 239 | this._formQuerySelector = options.form_query_selector || 'form'; 240 | this.utmCookie = new UtmCookie({ 241 | domain: options.domain, 242 | sessionLength: options.sessionLength, 243 | cookieExpiryDays: options.cookieExpiryDays, 244 | additionalParams: Object.getOwnPropertyNames(this._additionalParamsMap) 245 | }); 246 | this.addAllFields(); 247 | } 248 | 249 | UtmForm.prototype.addAllFields = function() { 250 | var allForms, i, len; 251 | allForms = document.querySelectorAll(this._formQuerySelector); 252 | if (this._addToForm === 'none') { 253 | len = 0; 254 | } else if (this._addToForm === 'first') { 255 | len = Math.min(1, allForms.length); 256 | } else { 257 | len = allForms.length; 258 | } 259 | i = 0; 260 | while (i < len) { 261 | this.addAllFieldsToForm(allForms[i]); 262 | i++; 263 | } 264 | }; 265 | 266 | UtmForm.prototype.addAllFieldsToForm = function(form) { 267 | var fieldName, param, ref, ref1; 268 | if (form && !form._utm_tagged) { 269 | form._utm_tagged = true; 270 | ref = this._utmParamsMap; 271 | for (param in ref) { 272 | fieldName = ref[param]; 273 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 274 | } 275 | ref1 = this._additionalParamsMap; 276 | for (param in ref1) { 277 | fieldName = ref1[param]; 278 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 279 | } 280 | this.addFormElem(form, this._initialReferrerField, this.utmCookie.initialReferrer()); 281 | this.addFormElem(form, this._lastReferrerField, this.utmCookie.lastReferrer()); 282 | this.addFormElem(form, this._initialLandingPageField, this.utmCookie.initialLandingPageUrl()); 283 | this.addFormElem(form, this._visitsField, this.utmCookie.visits()); 284 | } 285 | }; 286 | 287 | UtmForm.prototype.addFormElem = function(form, fieldName, fieldValue) { 288 | this.insertAfter(this.getFieldEl(fieldName, fieldValue), form.lastChild); 289 | }; 290 | 291 | UtmForm.prototype.getFieldEl = function(fieldName, fieldValue) { 292 | var fieldEl; 293 | fieldEl = document.createElement('input'); 294 | fieldEl.type = "hidden"; 295 | fieldEl.name = fieldName; 296 | fieldEl.value = fieldValue; 297 | return fieldEl; 298 | }; 299 | 300 | UtmForm.prototype.insertAfter = function(newNode, referenceNode) { 301 | return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 302 | }; 303 | 304 | return UtmForm; 305 | 306 | })(); 307 | 308 | _uf = window._uf || {}; 309 | 310 | window.UtmForm = new UtmForm(_uf); 311 | -------------------------------------------------------------------------------- /dest/utm_form-1.0.4.min.js: -------------------------------------------------------------------------------- 1 | var UtmCookie;UtmCookie=function(){function UtmCookie(options){null==options&&(options={}),this._cookieNamePrefix="_uc_",this._domain=options.domain,this._sessionLength=options.sessionLength||1,this._cookieExpiryDays=options.cookieExpiryDays||365,this._additionalParams=options.additionalParams||[],this._utmParams=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.writeInitialReferrer(),this.writeLastReferrer(),this.writeInitialLandingPageUrl(),this.setCurrentSession(),this.additionalParamsPresentInUrl()&&this.writeAdditionalParams(),this.utmPresentInUrl()&&this.writeUtmCookieFromParams()}return UtmCookie.prototype.createCookie=function(name,value,days,path,domain,secure){var cookieDomain,cookieExpire,cookiePath,cookieSecure,date,expireDate;expireDate=null,days&&(date=new Date,date.setTime(date.getTime()+24*days*60*60*1e3),expireDate=date),cookieExpire=null!=expireDate?"; expires="+expireDate.toGMTString():"",cookiePath=null!=path?"; path="+path:"; path=/",cookieDomain=null!=domain?"; domain="+domain:"",cookieSecure=null!=secure?"; secure":"",document.cookie=this._cookieNamePrefix+name+"="+escape(value)+cookieExpire+cookiePath+cookieDomain+cookieSecure},UtmCookie.prototype.readCookie=function(name){var c,ca,i,nameEQ;for(nameEQ=this._cookieNamePrefix+name+"=",ca=document.cookie.split(";"),i=0;ij;j++)if(param=ref[j],this.getParameterByName(param))return!0;return!1},UtmCookie.prototype.utmPresentInUrl=function(){var j,len,param,ref;for(ref=this._utmParams,j=0,len=ref.length;len>j;j++)if(param=ref[j],this.getParameterByName(param))return!0;return!1},UtmCookie.prototype.writeCookie=function(name,value){this.createCookie(name,value,this._cookieExpiryDays,null,this._domain)},UtmCookie.prototype.writeAdditionalParams=function(){var j,len,param,ref,value;for(ref=this._additionalParams,j=0,len=ref.length;len>j;j++)param=ref[j],value=this.getParameterByName(param),this.writeCookie(param,value)},UtmCookie.prototype.writeUtmCookieFromParams=function(){var j,len,param,ref,value;for(ref=this._utmParams,j=0,len=ref.length;len>j;j++)param=ref[j],value=this.getParameterByName(param),this.writeCookie(param,value)},UtmCookie.prototype.writeCookieOnce=function(name,value){var existingValue;existingValue=this.readCookie(name),existingValue||this.writeCookie(name,value)},UtmCookie.prototype._sameDomainReferrer=function(referrer){var hostname;return hostname=document.location.hostname,referrer.indexOf(this._domain)>-1||referrer.indexOf(hostname)>-1},UtmCookie.prototype._isInvalidReferrer=function(referrer){return""===referrer||void 0===referrer},UtmCookie.prototype.writeInitialReferrer=function(){var value;value=document.referrer,this._isInvalidReferrer(value)&&(value="direct"),this.writeCookieOnce("referrer",value)},UtmCookie.prototype.writeLastReferrer=function(){var value;value=document.referrer,this._sameDomainReferrer(value)||(this._isInvalidReferrer(value)&&(value="direct"),this.writeCookie("last_referrer",value))},UtmCookie.prototype.writeInitialLandingPageUrl=function(){var value;value=this.cleanUrl(),value&&this.writeCookieOnce("initial_landing_page",value)},UtmCookie.prototype.initialReferrer=function(){return this.readCookie("referrer")},UtmCookie.prototype.lastReferrer=function(){return this.readCookie("last_referrer")},UtmCookie.prototype.initialLandingPageUrl=function(){return this.readCookie("initial_landing_page")},UtmCookie.prototype.incrementVisitCount=function(){var cookieName,existingValue,newValue;cookieName="visits",existingValue=parseInt(this.readCookie(cookieName),10),newValue=1,newValue=isNaN(existingValue)?1:existingValue+1,this.writeCookie(cookieName,newValue)},UtmCookie.prototype.visits=function(){return this.readCookie("visits")},UtmCookie.prototype.setCurrentSession=function(){var cookieName,existingValue;cookieName="current_session",existingValue=this.readCookie(cookieName),existingValue||(this.createCookie(cookieName,"true",this._sessionLength/24,null,this._domain),this.incrementVisitCount())},UtmCookie.prototype.cleanUrl=function(){var cleanSearch;return cleanSearch=window.location.search.replace(/utm_[^&]+&?/g,"").replace(/&$/,"").replace(/^\?$/,""),window.location.origin+window.location.pathname+cleanSearch+window.location.hash},UtmCookie}();var UtmForm,_uf;UtmForm=function(){function UtmForm(options){null==options&&(options={}),this._utmParamsMap={},this._utmParamsMap.utm_source=options.utm_source_field||"USOURCE",this._utmParamsMap.utm_medium=options.utm_medium_field||"UMEDIUM",this._utmParamsMap.utm_campaign=options.utm_campaign_field||"UCAMPAIGN",this._utmParamsMap.utm_content=options.utm_content_field||"UCONTENT",this._utmParamsMap.utm_term=options.utm_term_field||"UTERM",this._additionalParamsMap=options.additional_params_map||{},this._initialReferrerField=options.initial_referrer_field||"IREFERRER",this._lastReferrerField=options.last_referrer_field||"LREFERRER",this._initialLandingPageField=options.initial_landing_page_field||"ILANDPAGE",this._visitsField=options.visits_field||"VISITS",this._addToForm=options.add_to_form||"all",this._formQuerySelector=options.form_query_selector||"form",this.utmCookie=new UtmCookie({domain:options.domain,sessionLength:options.sessionLength,cookieExpiryDays:options.cookieExpiryDays,additionalParams:Object.getOwnPropertyNames(this._additionalParamsMap)}),this.addAllFields()}return UtmForm.prototype.addAllFields=function(){var allForms,i,len;for(allForms=document.querySelectorAll(this._formQuerySelector),len="none"===this._addToForm?0:"first"===this._addToForm?Math.min(1,allForms.length):allForms.length,i=0;len>i;)this.addAllFieldsToForm(allForms[i]),i++},UtmForm.prototype.addAllFieldsToForm=function(form){var fieldName,param,ref,ref1;if(form&&!form._utm_tagged){form._utm_tagged=!0,ref=this._utmParamsMap;for(param in ref)fieldName=ref[param],this.addFormElem(form,fieldName,this.utmCookie.readCookie(param));ref1=this._additionalParamsMap;for(param in ref1)fieldName=ref1[param],this.addFormElem(form,fieldName,this.utmCookie.readCookie(param));this.addFormElem(form,this._initialReferrerField,this.utmCookie.initialReferrer()),this.addFormElem(form,this._lastReferrerField,this.utmCookie.lastReferrer()),this.addFormElem(form,this._initialLandingPageField,this.utmCookie.initialLandingPageUrl()),this.addFormElem(form,this._visitsField,this.utmCookie.visits())}},UtmForm.prototype.addFormElem=function(form,fieldName,fieldValue){this.insertAfter(this.getFieldEl(fieldName,fieldValue),form.lastChild)},UtmForm.prototype.getFieldEl=function(fieldName,fieldValue){var fieldEl;return fieldEl=document.createElement("input"),fieldEl.type="hidden",fieldEl.name=fieldName,fieldEl.value=fieldValue,fieldEl},UtmForm.prototype.insertAfter=function(newNode,referenceNode){return referenceNode.parentNode.insertBefore(newNode,referenceNode.nextSibling)},UtmForm}(),_uf=window._uf||{},window.UtmForm=new UtmForm(_uf); -------------------------------------------------------------------------------- /dest/utm_form-1.1.0.js: -------------------------------------------------------------------------------- 1 | var UtmCookie; 2 | 3 | UtmCookie = class UtmCookie { 4 | constructor(options = {}) { 5 | this._cookieNamePrefix = '_uc_'; 6 | this._domain = options.domain; 7 | this._sessionLength = options.sessionLength || 1; 8 | this._cookieExpiryDays = options.cookieExpiryDays || 365; 9 | this._additionalParams = options.additionalParams || []; 10 | this._utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; 11 | this.writeInitialReferrer(); 12 | this.writeLastReferrer(); 13 | this.writeInitialLandingPageUrl(); 14 | this.setCurrentSession(); 15 | if (this.additionalParamsPresentInUrl()) { 16 | this.writeAdditionalParams(); 17 | } 18 | if (this.utmPresentInUrl()) { 19 | this.writeUtmCookieFromParams(); 20 | } 21 | return; 22 | } 23 | 24 | createCookie(name, value, days, path, domain, secure) { 25 | var cookieDomain, cookieExpire, cookiePath, cookieSecure, date, expireDate; 26 | expireDate = null; 27 | if (days) { 28 | date = new Date; 29 | date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); 30 | expireDate = date; 31 | } 32 | cookieExpire = expireDate != null ? '; expires=' + expireDate.toGMTString() : ''; 33 | cookiePath = path != null ? '; path=' + path : '; path=/'; 34 | cookieDomain = domain != null ? '; domain=' + domain : ''; 35 | cookieSecure = secure != null ? '; secure' : ''; 36 | document.cookie = this._cookieNamePrefix + name + '=' + escape(value) + cookieExpire + cookiePath + cookieDomain + cookieSecure; 37 | } 38 | 39 | readCookie(name) { 40 | var c, ca, i, nameEQ; 41 | nameEQ = this._cookieNamePrefix + name + '='; 42 | ca = document.cookie.split(';'); 43 | i = 0; 44 | while (i < ca.length) { 45 | c = ca[i]; 46 | while (c.charAt(0) === ' ') { 47 | c = c.substring(1, c.length); 48 | } 49 | if (c.indexOf(nameEQ) === 0) { 50 | return c.substring(nameEQ.length, c.length); 51 | } 52 | i++; 53 | } 54 | return null; 55 | } 56 | 57 | eraseCookie(name) { 58 | this.createCookie(name, '', -1, null, this._domain); 59 | } 60 | 61 | getParameterByName(name) { 62 | var regex, regexS, results; 63 | name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); 64 | regexS = '[\\?&]' + name + '=([^&#]*)'; 65 | regex = new RegExp(regexS); 66 | results = regex.exec(window.location.search); 67 | if (results) { 68 | return decodeURIComponent(results[1].replace(/\+/g, ' ')); 69 | } else { 70 | return ''; 71 | } 72 | } 73 | 74 | additionalParamsPresentInUrl() { 75 | var j, len, param, ref; 76 | ref = this._additionalParams; 77 | for (j = 0, len = ref.length; j < len; j++) { 78 | param = ref[j]; 79 | if (this.getParameterByName(param)) { 80 | return true; 81 | } 82 | } 83 | return false; 84 | } 85 | 86 | utmPresentInUrl() { 87 | var j, len, param, ref; 88 | ref = this._utmParams; 89 | for (j = 0, len = ref.length; j < len; j++) { 90 | param = ref[j]; 91 | if (this.getParameterByName(param)) { 92 | return true; 93 | } 94 | } 95 | return false; 96 | } 97 | 98 | writeCookie(name, value) { 99 | this.createCookie(name, value, this._cookieExpiryDays, null, this._domain); 100 | } 101 | 102 | writeAdditionalParams() { 103 | var j, len, param, ref, value; 104 | ref = this._additionalParams; 105 | for (j = 0, len = ref.length; j < len; j++) { 106 | param = ref[j]; 107 | value = this.getParameterByName(param); 108 | this.writeCookie(param, value); 109 | } 110 | } 111 | 112 | writeUtmCookieFromParams() { 113 | var j, len, param, ref, value; 114 | ref = this._utmParams; 115 | for (j = 0, len = ref.length; j < len; j++) { 116 | param = ref[j]; 117 | value = this.getParameterByName(param); 118 | this.writeCookie(param, value); 119 | } 120 | } 121 | 122 | writeCookieOnce(name, value) { 123 | var existingValue; 124 | existingValue = this.readCookie(name); 125 | if (!existingValue) { 126 | this.writeCookie(name, value); 127 | } 128 | } 129 | 130 | _sameDomainReferrer(referrer) { 131 | var hostname; 132 | hostname = document.location.hostname; 133 | return referrer.indexOf(this._domain) > -1 || referrer.indexOf(hostname) > -1; 134 | } 135 | 136 | _isInvalidReferrer(referrer) { 137 | return referrer === '' || referrer === void 0; 138 | } 139 | 140 | writeInitialReferrer() { 141 | var value; 142 | value = document.referrer; 143 | if (this._isInvalidReferrer(value)) { 144 | value = 'direct'; 145 | } 146 | this.writeCookieOnce('referrer', value); 147 | } 148 | 149 | writeLastReferrer() { 150 | var value; 151 | value = document.referrer; 152 | if (!this._sameDomainReferrer(value)) { 153 | if (this._isInvalidReferrer(value)) { 154 | value = 'direct'; 155 | } 156 | this.writeCookie('last_referrer', value); 157 | } 158 | } 159 | 160 | writeInitialLandingPageUrl() { 161 | var value; 162 | value = this.cleanUrl(); 163 | if (value) { 164 | this.writeCookieOnce('initial_landing_page', value); 165 | } 166 | } 167 | 168 | initialReferrer() { 169 | return this.readCookie('referrer'); 170 | } 171 | 172 | lastReferrer() { 173 | return this.readCookie('last_referrer'); 174 | } 175 | 176 | initialLandingPageUrl() { 177 | return this.readCookie('initial_landing_page'); 178 | } 179 | 180 | incrementVisitCount() { 181 | var cookieName, existingValue, newValue; 182 | cookieName = 'visits'; 183 | existingValue = parseInt(this.readCookie(cookieName), 10); 184 | newValue = 1; 185 | if (isNaN(existingValue)) { 186 | newValue = 1; 187 | } else { 188 | newValue = existingValue + 1; 189 | } 190 | this.writeCookie(cookieName, newValue); 191 | } 192 | 193 | visits() { 194 | return this.readCookie('visits'); 195 | } 196 | 197 | setCurrentSession() { 198 | var cookieName, existingValue; 199 | cookieName = 'current_session'; 200 | existingValue = this.readCookie(cookieName); 201 | if (!existingValue) { 202 | this.createCookie(cookieName, 'true', this._sessionLength / 24, null, this._domain); 203 | this.incrementVisitCount(); 204 | } 205 | } 206 | 207 | cleanUrl() { 208 | var cleanSearch; 209 | cleanSearch = window.location.search.replace(/utm_[^&]+&?/g, '').replace(/&$/, '').replace(/^\?$/, ''); 210 | return window.location.origin + window.location.pathname + cleanSearch + window.location.hash; 211 | } 212 | 213 | }; 214 | 215 | var UtmForm, _uf; 216 | 217 | UtmForm = class UtmForm { 218 | constructor(options = {}) { 219 | this._utmParamsMap = {}; 220 | this._utmParamsMap.utm_source = options.utm_source_field || 'USOURCE'; 221 | this._utmParamsMap.utm_medium = options.utm_medium_field || 'UMEDIUM'; 222 | this._utmParamsMap.utm_campaign = options.utm_campaign_field || 'UCAMPAIGN'; 223 | this._utmParamsMap.utm_content = options.utm_content_field || 'UCONTENT'; 224 | this._utmParamsMap.utm_term = options.utm_term_field || 'UTERM'; 225 | this._additionalParamsMap = options.additional_params_map || {}; 226 | this._initialReferrerField = options.initial_referrer_field || 'IREFERRER'; 227 | this._lastReferrerField = options.last_referrer_field || 'LREFERRER'; 228 | this._initialLandingPageField = options.initial_landing_page_field || 'ILANDPAGE'; 229 | this._visitsField = options.visits_field || 'VISITS'; 230 | // Options: 231 | // "none": Don't add any fields to any form 232 | // "first": Add UTM and other fields to only first form on the page 233 | // "all": (Default) Add UTM and other fields to all forms on the page 234 | this._addToForm = options.add_to_form || 'all'; 235 | this._formQuerySelector = options.form_query_selector || 'form'; 236 | this._decodeURIs = options.decode_uris || false; 237 | this.utmCookie = new UtmCookie({ 238 | domain: options.domain, 239 | sessionLength: options.sessionLength, 240 | cookieExpiryDays: options.cookieExpiryDays, 241 | additionalParams: Object.getOwnPropertyNames(this._additionalParamsMap) 242 | }); 243 | this.addAllFields(); 244 | } 245 | 246 | addAllFields() { 247 | var allForms, i, len; 248 | allForms = document.querySelectorAll(this._formQuerySelector); 249 | if (this._addToForm === 'none') { 250 | len = 0; 251 | } else if (this._addToForm === 'first') { 252 | len = Math.min(1, allForms.length); 253 | } else { 254 | len = allForms.length; 255 | } 256 | i = 0; 257 | while (i < len) { 258 | this.addAllFieldsToForm(allForms[i]); 259 | i++; 260 | } 261 | } 262 | 263 | addAllFieldsToForm(form) { 264 | var fieldName, param, ref, ref1; 265 | if (form && !form._utm_tagged) { 266 | form._utm_tagged = true; 267 | ref = this._utmParamsMap; 268 | for (param in ref) { 269 | fieldName = ref[param]; 270 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 271 | } 272 | ref1 = this._additionalParamsMap; 273 | for (param in ref1) { 274 | fieldName = ref1[param]; 275 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 276 | } 277 | this.addFormElem(form, this._initialReferrerField, this.utmCookie.initialReferrer()); 278 | this.addFormElem(form, this._lastReferrerField, this.utmCookie.lastReferrer()); 279 | this.addFormElem(form, this._initialLandingPageField, this.utmCookie.initialLandingPageUrl()); 280 | this.addFormElem(form, this._visitsField, this.utmCookie.visits()); 281 | } 282 | } 283 | 284 | addFormElem(form, fieldName, fieldValue) { 285 | this.insertAfter(this.getFieldEl(fieldName, fieldValue), form.lastChild); 286 | } 287 | 288 | // NOTE: This should be called for each form element or since it 289 | // attaches itself to the first form 290 | getFieldEl(fieldName, fieldValue) { 291 | var fieldEl; 292 | fieldEl = document.createElement('input'); 293 | fieldEl.type = "hidden"; 294 | fieldEl.name = fieldName; 295 | fieldEl.value = this._decodeURIs ? decodeURIComponent(fieldValue) : fieldValue; 296 | return fieldEl; 297 | } 298 | 299 | insertAfter(newNode, referenceNode) { 300 | return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 301 | } 302 | 303 | }; 304 | 305 | _uf = window._uf || {}; 306 | 307 | window.UtmForm = new UtmForm(_uf); 308 | -------------------------------------------------------------------------------- /dest/utm_form-1.1.0.min.js: -------------------------------------------------------------------------------- 1 | var UtmCookie,UtmForm,_uf;UtmCookie=class{constructor(options={}){this._cookieNamePrefix="_uc_",this._domain=options.domain,this._sessionLength=options.sessionLength||1,this._cookieExpiryDays=options.cookieExpiryDays||365,this._additionalParams=options.additionalParams||[],this._utmParams=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.writeInitialReferrer(),this.writeLastReferrer(),this.writeInitialLandingPageUrl(),this.setCurrentSession(),this.additionalParamsPresentInUrl()&&this.writeAdditionalParams(),this.utmPresentInUrl()&&this.writeUtmCookieFromParams()}createCookie(name,value,days,path,domain,secure){var cookieDomain,cookieExpire,cookiePath,cookieSecure,date,expireDate;expireDate=null,days&&((date=new Date).setTime(date.getTime()+24*days*60*60*1e3),expireDate=date),cookieExpire=null!=expireDate?"; expires="+expireDate.toGMTString():"",cookiePath=null!=path?"; path="+path:"; path=/",cookieDomain=null!=domain?"; domain="+domain:"",cookieSecure=null!=secure?"; secure":"",document.cookie=this._cookieNamePrefix+name+"="+escape(value)+cookieExpire+cookiePath+cookieDomain+cookieSecure}readCookie(name){var c,ca,i,nameEQ;for(nameEQ=this._cookieNamePrefix+name+"=",ca=document.cookie.split(";"),i=0;i-1||referrer.indexOf(hostname)>-1}_isInvalidReferrer(referrer){return""===referrer||void 0===referrer}writeInitialReferrer(){var value;value=document.referrer,this._isInvalidReferrer(value)&&(value="direct"),this.writeCookieOnce("referrer",value)}writeLastReferrer(){var value;value=document.referrer,this._sameDomainReferrer(value)||(this._isInvalidReferrer(value)&&(value="direct"),this.writeCookie("last_referrer",value))}writeInitialLandingPageUrl(){var value;(value=this.cleanUrl())&&this.writeCookieOnce("initial_landing_page",value)}initialReferrer(){return this.readCookie("referrer")}lastReferrer(){return this.readCookie("last_referrer")}initialLandingPageUrl(){return this.readCookie("initial_landing_page")}incrementVisitCount(){var existingValue,newValue;existingValue=parseInt(this.readCookie("visits"),10),newValue=1,newValue=isNaN(existingValue)?1:existingValue+1,this.writeCookie("visits",newValue)}visits(){return this.readCookie("visits")}setCurrentSession(){this.readCookie("current_session")||(this.createCookie("current_session","true",this._sessionLength/24,null,this._domain),this.incrementVisitCount())}cleanUrl(){var cleanSearch;return cleanSearch=window.location.search.replace(/utm_[^&]+&?/g,"").replace(/&$/,"").replace(/^\?$/,""),window.location.origin+window.location.pathname+cleanSearch+window.location.hash}},UtmForm=class{constructor(options={}){this._utmParamsMap={},this._utmParamsMap.utm_source=options.utm_source_field||"USOURCE",this._utmParamsMap.utm_medium=options.utm_medium_field||"UMEDIUM",this._utmParamsMap.utm_campaign=options.utm_campaign_field||"UCAMPAIGN",this._utmParamsMap.utm_content=options.utm_content_field||"UCONTENT",this._utmParamsMap.utm_term=options.utm_term_field||"UTERM",this._additionalParamsMap=options.additional_params_map||{},this._initialReferrerField=options.initial_referrer_field||"IREFERRER",this._lastReferrerField=options.last_referrer_field||"LREFERRER",this._initialLandingPageField=options.initial_landing_page_field||"ILANDPAGE",this._visitsField=options.visits_field||"VISITS",this._addToForm=options.add_to_form||"all",this._formQuerySelector=options.form_query_selector||"form",this._decodeURIs=options.decode_uris||!1,this.utmCookie=new UtmCookie({domain:options.domain,sessionLength:options.sessionLength,cookieExpiryDays:options.cookieExpiryDays,additionalParams:Object.getOwnPropertyNames(this._additionalParamsMap)}),this.addAllFields()}addAllFields(){var allForms,i,len;for(allForms=document.querySelectorAll(this._formQuerySelector),len="none"===this._addToForm?0:"first"===this._addToForm?Math.min(1,allForms.length):allForms.length,i=0;i -1 || referrer.indexOf(hostname) > -1; 163 | } 164 | 165 | _isInvalidReferrer(referrer) { 166 | return referrer === '' || referrer === void 0; 167 | } 168 | 169 | writeInitialReferrer() { 170 | var value; 171 | value = document.referrer; 172 | if (this._isInvalidReferrer(value)) { 173 | value = 'direct'; 174 | } 175 | this.writeCookieOnce('referrer', value); 176 | } 177 | 178 | writeLastReferrer() { 179 | var value; 180 | value = document.referrer; 181 | if (!this._sameDomainReferrer(value)) { 182 | if (this._isInvalidReferrer(value)) { 183 | value = 'direct'; 184 | } 185 | this.writeCookie('last_referrer', value); 186 | } 187 | } 188 | 189 | writeInitialLandingPageUrl() { 190 | var value; 191 | value = this.cleanUrl(); 192 | if (value) { 193 | this.writeCookieOnce('initial_landing_page', value); 194 | } 195 | } 196 | 197 | initialReferrer() { 198 | return this.readCookie('referrer'); 199 | } 200 | 201 | lastReferrer() { 202 | return this.readCookie('last_referrer'); 203 | } 204 | 205 | initialLandingPageUrl() { 206 | return this.readCookie('initial_landing_page'); 207 | } 208 | 209 | incrementVisitCount() { 210 | var cookieName, existingValue, newValue; 211 | cookieName = 'visits'; 212 | existingValue = parseInt(this.readCookie(cookieName), 10); 213 | if (isNaN(existingValue)) { 214 | newValue = 1; 215 | } else { 216 | newValue = existingValue + 1; 217 | } 218 | this.writeCookie(cookieName, newValue); 219 | } 220 | 221 | visits() { 222 | return this.readCookie('visits'); 223 | } 224 | 225 | setCurrentSession() { 226 | var cookieName, existingValue; 227 | cookieName = 'current_session'; 228 | existingValue = this.readCookie(cookieName); 229 | if (!existingValue) { 230 | this.createCookie(cookieName, 'true', this._sessionLength / 24, null, this._domain, this._secure); 231 | this.incrementVisitCount(); 232 | } 233 | } 234 | 235 | cleanUrl() { 236 | var cleanSearch; 237 | cleanSearch = window.location.search.replace(/utm_[^&]+&?/g, '').replace(/&$/, '').replace(/^\?$/, ''); 238 | return window.location.origin + window.location.pathname + cleanSearch + window.location.hash; 239 | } 240 | 241 | }; 242 | 243 | var UtmForm, _uf; 244 | 245 | UtmForm = class UtmForm { 246 | constructor(options = {}) { 247 | this._utmParamsMap = {}; 248 | this._utmParamsMap.utm_source = options.utm_source_field || 'USOURCE'; 249 | this._utmParamsMap.utm_medium = options.utm_medium_field || 'UMEDIUM'; 250 | this._utmParamsMap.utm_campaign = options.utm_campaign_field || 'UCAMPAIGN'; 251 | this._utmParamsMap.utm_content = options.utm_content_field || 'UCONTENT'; 252 | this._utmParamsMap.utm_term = options.utm_term_field || 'UTERM'; 253 | this._initialUtmParamsMap = {}; 254 | this._initialUtmParamsMap.initial_utm_source = options.initial_utm_source_field || 'IUSOURCE'; 255 | this._initialUtmParamsMap.initial_utm_medium = options.initial_utm_medium_field || 'IUMEDIUM'; 256 | this._initialUtmParamsMap.initial_utm_campaign = options.initial_utm_campaign_field || 'IUCAMPAIGN'; 257 | this._initialUtmParamsMap.initial_utm_content = options.initial_utm_content_field || 'IUCONTENT'; 258 | this._initialUtmParamsMap.initial_utm_term = options.initial_utm_term_field || 'IUTERM'; 259 | this._additionalParamsMap = options.additional_params_map || {}; 260 | this._additionalInitialParamsMap = options.additional_initial_params_map || {}; 261 | this._initialReferrerField = options.initial_referrer_field || 'IREFERRER'; 262 | this._lastReferrerField = options.last_referrer_field || 'LREFERRER'; 263 | this._initialLandingPageField = options.initial_landing_page_field || 'ILANDPAGE'; 264 | this._visitsField = options.visits_field || 'VISITS'; 265 | // Options: 266 | // "none": Don't add any fields to any form 267 | // "first": Add UTM and other fields to only first form on the page 268 | // "all": (Default) Add UTM and other fields to all forms on the page 269 | this._addToForm = options.add_to_form || 'all'; 270 | this._formQuerySelector = options.form_query_selector || 'form'; 271 | this._decodeURIs = options.decode_uris || false; 272 | this.utmCookie = new UtmCookie({ 273 | domain: options.domain, 274 | secure: options.secure, 275 | sessionLength: options.sessionLength, 276 | cookieExpiryDays: options.cookieExpiryDays, 277 | initialUtmParams: options.initial_utm_params, 278 | additionalParams: Object.getOwnPropertyNames(this._additionalParamsMap), 279 | additionalInitialParams: Object.getOwnPropertyNames(this._additionalInitialParamsMap) 280 | }); 281 | this.addAllFields(); 282 | } 283 | 284 | addAllFields() { 285 | var allForms, i, len; 286 | allForms = document.querySelectorAll(this._formQuerySelector); 287 | if (this._addToForm === 'none') { 288 | len = 0; 289 | } else if (this._addToForm === 'first') { 290 | len = Math.min(1, allForms.length); 291 | } else { 292 | len = allForms.length; 293 | } 294 | i = 0; 295 | while (i < len) { 296 | this.addAllFieldsToForm(allForms[i]); 297 | i++; 298 | } 299 | } 300 | 301 | addAllFieldsToForm(form) { 302 | var cookieName, fieldName, param, ref, ref1, ref2, ref3; 303 | if (form && !form._utm_tagged) { 304 | form._utm_tagged = true; 305 | ref = this._utmParamsMap; 306 | for (param in ref) { 307 | fieldName = ref[param]; 308 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 309 | } 310 | ref1 = this._initialUtmParamsMap; 311 | for (param in ref1) { 312 | fieldName = ref1[param]; 313 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 314 | } 315 | ref2 = this._additionalParamsMap; 316 | for (param in ref2) { 317 | fieldName = ref2[param]; 318 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(param)); 319 | } 320 | ref3 = this._additionalInitialParamsMap; 321 | for (param in ref3) { 322 | fieldName = ref3[param]; 323 | cookieName = 'initial_' + param; 324 | this.addFormElem(form, fieldName, this.utmCookie.readCookie(cookieName)); 325 | } 326 | this.addFormElem(form, this._initialReferrerField, this.utmCookie.initialReferrer()); 327 | this.addFormElem(form, this._lastReferrerField, this.utmCookie.lastReferrer()); 328 | this.addFormElem(form, this._initialLandingPageField, this.utmCookie.initialLandingPageUrl()); 329 | this.addFormElem(form, this._visitsField, this.utmCookie.visits()); 330 | } 331 | } 332 | 333 | addFormElem(form, fieldName, fieldValue) { 334 | this.insertAfter(this.getFieldEl(fieldName, fieldValue), form.lastChild); 335 | } 336 | 337 | // NOTE: This should be called for each form element or since it 338 | // attaches itself to the first form 339 | getFieldEl(fieldName, fieldValue) { 340 | var fieldEl; 341 | fieldEl = document.createElement('input'); 342 | fieldEl.type = "hidden"; 343 | fieldEl.name = fieldName; 344 | fieldEl.value = this._decodeURIs ? decodeURIComponent(fieldValue) : fieldValue; 345 | return fieldEl; 346 | } 347 | 348 | insertAfter(newNode, referenceNode) { 349 | return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 350 | } 351 | 352 | }; 353 | 354 | _uf = window._uf || {}; 355 | 356 | window.UtmForm = new UtmForm(_uf); 357 | -------------------------------------------------------------------------------- /dest/utm_form-1.2.0.min.js: -------------------------------------------------------------------------------- 1 | var UtmCookie,UtmForm,_uf;UtmCookie=class{constructor(options={}){this._cookieNamePrefix="_uc_",this._domain=options.domain,this._secure=options.secure||!1,this._initialUtmParams=options.initialUtmParams||!1,this._sessionLength=options.sessionLength||1,this._cookieExpiryDays=options.cookieExpiryDays||365,this._additionalParams=options.additionalParams||[],this._additionalInitialParams=options.additionalInitialParams||[],this._utmParams=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.writeInitialReferrer(),this.writeLastReferrer(),this.writeInitialLandingPageUrl(),this.writeAdditionalInitialParams(),this.setCurrentSession(),this._initialUtmParams&&this.writeInitialUtmCookieFromParams(),this.additionalParamsPresentInUrl()&&this.writeAdditionalParams(),this.utmPresentInUrl()&&this.writeUtmCookieFromParams()}createCookie(name,value,days,path,domain,secure){var cookieDomain,cookieExpire,cookiePath,cookieSecure,date,expireDate;expireDate=null,days&&((date=new Date).setTime(date.getTime()+24*days*60*60*1e3),expireDate=date),cookieExpire=null!=expireDate?"; expires="+expireDate.toGMTString():"",cookiePath=null!=path?"; path="+path:"; path=/",cookieDomain=null!=domain?"; domain="+domain:"",cookieSecure=secure?"; secure":"",document.cookie=this._cookieNamePrefix+name+"="+escape(value)+cookieExpire+cookiePath+cookieDomain+cookieSecure}readCookie(name){var c,ca,i,nameEQ;for(nameEQ=this._cookieNamePrefix+name+"=",ca=document.cookie.split(";"),i=0;i-1||referrer.indexOf(hostname)>-1}_isInvalidReferrer(referrer){return""===referrer||void 0===referrer}writeInitialReferrer(){var value;value=document.referrer,this._isInvalidReferrer(value)&&(value="direct"),this.writeCookieOnce("referrer",value)}writeLastReferrer(){var value;value=document.referrer,this._sameDomainReferrer(value)||(this._isInvalidReferrer(value)&&(value="direct"),this.writeCookie("last_referrer",value))}writeInitialLandingPageUrl(){var value;(value=this.cleanUrl())&&this.writeCookieOnce("initial_landing_page",value)}initialReferrer(){return this.readCookie("referrer")}lastReferrer(){return this.readCookie("last_referrer")}initialLandingPageUrl(){return this.readCookie("initial_landing_page")}incrementVisitCount(){var existingValue,newValue;existingValue=parseInt(this.readCookie("visits"),10),newValue=isNaN(existingValue)?1:existingValue+1,this.writeCookie("visits",newValue)}visits(){return this.readCookie("visits")}setCurrentSession(){this.readCookie("current_session")||(this.createCookie("current_session","true",this._sessionLength/24,null,this._domain,this._secure),this.incrementVisitCount())}cleanUrl(){var cleanSearch;return cleanSearch=window.location.search.replace(/utm_[^&]+&?/g,"").replace(/&$/,"").replace(/^\?$/,""),window.location.origin+window.location.pathname+cleanSearch+window.location.hash}},UtmForm=class{constructor(options={}){this._utmParamsMap={},this._utmParamsMap.utm_source=options.utm_source_field||"USOURCE",this._utmParamsMap.utm_medium=options.utm_medium_field||"UMEDIUM",this._utmParamsMap.utm_campaign=options.utm_campaign_field||"UCAMPAIGN",this._utmParamsMap.utm_content=options.utm_content_field||"UCONTENT",this._utmParamsMap.utm_term=options.utm_term_field||"UTERM",this._initialUtmParamsMap={},this._initialUtmParamsMap.initial_utm_source=options.initial_utm_source_field||"IUSOURCE",this._initialUtmParamsMap.initial_utm_medium=options.initial_utm_medium_field||"IUMEDIUM",this._initialUtmParamsMap.initial_utm_campaign=options.initial_utm_campaign_field||"IUCAMPAIGN",this._initialUtmParamsMap.initial_utm_content=options.initial_utm_content_field||"IUCONTENT",this._initialUtmParamsMap.initial_utm_term=options.initial_utm_term_field||"IUTERM",this._additionalParamsMap=options.additional_params_map||{},this._additionalInitialParamsMap=options.additional_initial_params_map||{},this._initialReferrerField=options.initial_referrer_field||"IREFERRER",this._lastReferrerField=options.last_referrer_field||"LREFERRER",this._initialLandingPageField=options.initial_landing_page_field||"ILANDPAGE",this._visitsField=options.visits_field||"VISITS",this._addToForm=options.add_to_form||"all",this._formQuerySelector=options.form_query_selector||"form",this._decodeURIs=options.decode_uris||!1,this.utmCookie=new UtmCookie({domain:options.domain,secure:options.secure,sessionLength:options.sessionLength,cookieExpiryDays:options.cookieExpiryDays,initialUtmParams:options.initial_utm_params,additionalParams:Object.getOwnPropertyNames(this._additionalParamsMap),additionalInitialParams:Object.getOwnPropertyNames(this._additionalInitialParamsMap)}),this.addAllFields()}addAllFields(){var allForms,i,len;for(allForms=document.querySelectorAll(this._formQuerySelector),len="none"===this._addToForm?0:"first"===this._addToForm?Math.min(1,allForms.length):allForms.length,i=0;i 3 | @_cookieNamePrefix = '_uc_' 4 | @_domain = options.domain 5 | @_secure = options.secure || false 6 | @_initialUtmParams = options.initialUtmParams || false 7 | @_sessionLength = options.sessionLength || 1 8 | @_cookieExpiryDays = options.cookieExpiryDays || 365 9 | @_additionalParams = options.additionalParams || [] 10 | @_additionalInitialParams = options.additionalInitialParams || [] 11 | @_utmParams = [ 12 | 'utm_source' 13 | 'utm_medium' 14 | 'utm_campaign' 15 | 'utm_term' 16 | 'utm_content' 17 | ] 18 | 19 | @writeInitialReferrer() 20 | @writeLastReferrer() 21 | @writeInitialLandingPageUrl() 22 | @writeAdditionalInitialParams() 23 | @setCurrentSession() 24 | 25 | if @_initialUtmParams 26 | @writeInitialUtmCookieFromParams() 27 | 28 | if @additionalParamsPresentInUrl() 29 | @writeAdditionalParams() 30 | 31 | if @utmPresentInUrl() 32 | @writeUtmCookieFromParams() 33 | 34 | return 35 | 36 | createCookie: (name, value, days, path, domain, secure) -> 37 | expireDate = null 38 | if days 39 | date = new Date 40 | date.setTime date.getTime() + days * 24 * 60 * 60 * 1000 41 | expireDate = date 42 | 43 | cookieExpire = if expireDate? then '; expires=' + expireDate.toGMTString() else '' 44 | cookiePath = if path? then '; path=' + path else '; path=/' 45 | cookieDomain = if domain? then '; domain=' + domain else '' 46 | cookieSecure = if secure then '; secure' else '' 47 | document.cookie = @_cookieNamePrefix + name + '=' + escape(value) + cookieExpire + cookiePath + cookieDomain + cookieSecure 48 | return 49 | 50 | readCookie: (name) -> 51 | nameEQ = @_cookieNamePrefix + name + '=' 52 | ca = document.cookie.split(';') 53 | i = 0 54 | while i < ca.length 55 | c = ca[i] 56 | while c.charAt(0) == ' ' 57 | c = c.substring(1, c.length) 58 | if c.indexOf(nameEQ) == 0 59 | return c.substring(nameEQ.length, c.length) 60 | i++ 61 | null 62 | 63 | eraseCookie: (name) -> 64 | @createCookie name, '', -1, null, @_domain, @_secure 65 | return 66 | 67 | getParameterByName: (name) -> 68 | name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]') 69 | regexS = '[\\?&]' + name + '=([^&#]*)' 70 | regex = new RegExp(regexS) 71 | results = regex.exec(window.location.search) 72 | if results 73 | decodeURIComponent results[1].replace(/\+/g, ' ') 74 | else 75 | '' 76 | 77 | additionalParamsPresentInUrl: -> 78 | for param in @_additionalParams 79 | if @getParameterByName(param) 80 | return true 81 | return false 82 | 83 | utmPresentInUrl: -> 84 | for param in @_utmParams 85 | if @getParameterByName(param) 86 | return true 87 | return false 88 | 89 | writeCookie: (name, value) -> 90 | @createCookie name, value, @_cookieExpiryDays, null, @_domain, @_secure 91 | return 92 | 93 | writeCookieOnce: (name, value) -> 94 | existingValue = @readCookie(name) 95 | if !existingValue 96 | @writeCookie name, value 97 | return 98 | 99 | writeAdditionalParams: -> 100 | for param in @_additionalParams 101 | value = @getParameterByName(param) 102 | @writeCookie param, value 103 | return 104 | 105 | writeAdditionalInitialParams: -> 106 | for param in @_additionalInitialParams 107 | name = 'initial_' + param 108 | value = @getParameterByName(param) || null 109 | @writeCookieOnce name, value 110 | return 111 | 112 | writeUtmCookieFromParams: -> 113 | for param in @_utmParams 114 | value = @getParameterByName(param) 115 | @writeCookie param, value 116 | return 117 | 118 | writeInitialUtmCookieFromParams: -> 119 | for param in @_utmParams 120 | name = 'initial_' + param 121 | value = @getParameterByName(param) || null 122 | @writeCookieOnce name, value 123 | return 124 | 125 | _sameDomainReferrer: (referrer) -> 126 | hostname = document.location.hostname 127 | referrer.indexOf(@_domain) > -1 or referrer.indexOf(hostname) > -1 128 | 129 | _isInvalidReferrer: (referrer) -> 130 | referrer == '' or referrer == undefined 131 | 132 | writeInitialReferrer: -> 133 | value = document.referrer 134 | if @_isInvalidReferrer(value) 135 | value = 'direct' 136 | @writeCookieOnce 'referrer', value 137 | return 138 | 139 | writeLastReferrer: -> 140 | value = document.referrer 141 | if !@_sameDomainReferrer(value) 142 | if @_isInvalidReferrer(value) 143 | value = 'direct' 144 | @writeCookie 'last_referrer', value 145 | return 146 | 147 | writeInitialLandingPageUrl: -> 148 | value = @cleanUrl() 149 | if value 150 | @writeCookieOnce 'initial_landing_page', value 151 | return 152 | 153 | initialReferrer: -> 154 | @readCookie 'referrer' 155 | 156 | lastReferrer: -> 157 | @readCookie 'last_referrer' 158 | 159 | initialLandingPageUrl: -> 160 | @readCookie 'initial_landing_page' 161 | 162 | incrementVisitCount: -> 163 | cookieName = 'visits' 164 | existingValue = parseInt(@readCookie(cookieName), 10) 165 | if isNaN(existingValue) 166 | newValue = 1 167 | else 168 | newValue = existingValue + 1 169 | @writeCookie cookieName, newValue 170 | return 171 | 172 | visits: -> 173 | @readCookie 'visits' 174 | 175 | setCurrentSession: -> 176 | cookieName = 'current_session' 177 | existingValue = @readCookie(cookieName) 178 | if !existingValue 179 | @createCookie cookieName, 'true', @_sessionLength / 24, null, @_domain, @_secure 180 | @incrementVisitCount() 181 | return 182 | 183 | cleanUrl: -> 184 | cleanSearch = window.location.search. 185 | replace(/utm_[^&]+&?/g, ''). 186 | replace(/&$/, ''). 187 | replace(/^\?$/, '') 188 | 189 | window.location.origin + window.location.pathname + cleanSearch + window.location.hash 190 | -------------------------------------------------------------------------------- /lib/utm_form.coffee: -------------------------------------------------------------------------------- 1 | class UtmForm 2 | constructor: (options = {}) -> 3 | @_utmParamsMap = {} 4 | @_utmParamsMap.utm_source = options.utm_source_field || 'USOURCE' 5 | @_utmParamsMap.utm_medium = options.utm_medium_field || 'UMEDIUM' 6 | @_utmParamsMap.utm_campaign = options.utm_campaign_field || 'UCAMPAIGN' 7 | @_utmParamsMap.utm_content = options.utm_content_field || 'UCONTENT' 8 | @_utmParamsMap.utm_term = options.utm_term_field || 'UTERM' 9 | 10 | @_initialUtmParamsMap = {} 11 | @_initialUtmParamsMap.initial_utm_source = options.initial_utm_source_field || 'IUSOURCE' 12 | @_initialUtmParamsMap.initial_utm_medium = options.initial_utm_medium_field || 'IUMEDIUM' 13 | @_initialUtmParamsMap.initial_utm_campaign = options.initial_utm_campaign_field || 'IUCAMPAIGN' 14 | @_initialUtmParamsMap.initial_utm_content = options.initial_utm_content_field || 'IUCONTENT' 15 | @_initialUtmParamsMap.initial_utm_term = options.initial_utm_term_field || 'IUTERM' 16 | 17 | @_additionalParamsMap = options.additional_params_map || {} 18 | @_additionalInitialParamsMap = options.additional_initial_params_map || {} 19 | 20 | @_initialReferrerField = options.initial_referrer_field || 'IREFERRER' 21 | @_lastReferrerField = options.last_referrer_field || 'LREFERRER' 22 | @_initialLandingPageField = options.initial_landing_page_field || 'ILANDPAGE' 23 | @_visitsField = options.visits_field || 'VISITS' 24 | 25 | # Options: 26 | # "none": Don't add any fields to any form 27 | # "first": Add UTM and other fields to only first form on the page 28 | # "all": (Default) Add UTM and other fields to all forms on the page 29 | @_addToForm = options.add_to_form || 'all'; 30 | 31 | # Option to configure the form query selector to further restrict form 32 | # decoration, e.g. 'form[action="/sign_up"]' 33 | @_formQuerySelector = options.form_query_selector || 'form'; 34 | 35 | # If enabled, value="" fields will be run through 36 | # decodeURIComponent() first before being outputted. This fixes 37 | # URLs looking strange, such as "https%3A//" instead of "https://" 38 | @_decodeURIs = options.decode_uris || false; 39 | 40 | @utmCookie = new UtmCookie({ 41 | domain: options.domain, 42 | secure: options.secure, 43 | sessionLength: options.sessionLength, 44 | cookieExpiryDays: options.cookieExpiryDays, 45 | initialUtmParams: options.initial_utm_params, 46 | additionalParams: Object.getOwnPropertyNames(@_additionalParamsMap), 47 | additionalInitialParams: Object.getOwnPropertyNames(@_additionalInitialParamsMap) }) 48 | 49 | @addAllFields() 50 | 51 | addAllFields: -> 52 | allForms = document.querySelectorAll(@_formQuerySelector) 53 | if @_addToForm == 'none' 54 | len = 0 55 | else if @_addToForm == 'first' 56 | len = Math.min 1, allForms.length 57 | else 58 | len = allForms.length 59 | 60 | i = 0 61 | while i < len 62 | @addAllFieldsToForm allForms[i] 63 | i++ 64 | 65 | return 66 | 67 | addAllFieldsToForm: (form) -> 68 | if form && !form._utm_tagged 69 | form._utm_tagged = true 70 | 71 | for param, fieldName of @_utmParamsMap 72 | @addFormElem form, fieldName, @utmCookie.readCookie(param) 73 | 74 | for param, fieldName of @_initialUtmParamsMap 75 | @addFormElem form, fieldName, @utmCookie.readCookie(param) 76 | 77 | for param, fieldName of @_additionalParamsMap 78 | @addFormElem form, fieldName, @utmCookie.readCookie(param) 79 | 80 | for param, fieldName of @_additionalInitialParamsMap 81 | cookieName = 'initial_' + param 82 | @addFormElem form, fieldName, @utmCookie.readCookie(cookieName) 83 | 84 | @addFormElem form, @_initialReferrerField, @utmCookie.initialReferrer() 85 | @addFormElem form, @_lastReferrerField, @utmCookie.lastReferrer() 86 | @addFormElem form, @_initialLandingPageField, @utmCookie.initialLandingPageUrl() 87 | @addFormElem form, @_visitsField, @utmCookie.visits() 88 | 89 | return 90 | 91 | addFormElem: (form, fieldName, fieldValue) -> 92 | @insertAfter @getFieldEl(fieldName, fieldValue), form.lastChild 93 | 94 | return 95 | 96 | # NOTE: This should be called for each form element or since it 97 | # attaches itself to the first form 98 | getFieldEl: (fieldName, fieldValue) -> 99 | fieldEl = document.createElement('input') 100 | fieldEl.type = "hidden" 101 | fieldEl.name = fieldName 102 | fieldEl.value = if @_decodeURIs then decodeURIComponent(fieldValue) else fieldValue 103 | fieldEl 104 | 105 | insertAfter: (newNode, referenceNode) -> 106 | referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling) 107 | 108 | _uf = window._uf || {} 109 | window.UtmForm = new UtmForm _uf 110 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "utm-form", 3 | "version": "1.2.0", 4 | "description": "Add UTM parameters and other information to forms", 5 | "main": "", 6 | "scripts": { 7 | "build": "gulp build", 8 | "test": "" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "" 13 | }, 14 | "author": "Puru Choudhary (https://github.com/medius)", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "" 18 | }, 19 | "devDependencies": { 20 | "fancy-log": "~1.3.3", 21 | "gulp": "^4.0.2", 22 | "gulp-clean": "^0.4.0", 23 | "gulp-coffee": "~3.0.3", 24 | "gulp-concat": "~2.6.1", 25 | "gulp-rename": "~2.0.0", 26 | "gulp-uglify-es": "~2.0.0" 27 | } 28 | } 29 | --------------------------------------------------------------------------------