Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function UTMParserFormFill(
- sourceSelector,
- mediumSelector,
- campaignSelector,
- contentSelector,
- termSelector,
- utm_adgroup ){
- // SEARCH PARAMETERS
- // search for any utm parameters within the query string
- // create a cookie from those utm query string key values
- for (var i = sourceSelector.length - 1; i >= 0; i--) {
- // create function that establishes cookie from query string key / channel type values
- function createCookie(utmcookie){
- var now = new Date();
- var time = now.getTime();
- var expireTime = time + 1000*60*15;
- now.setTime(expireTime);
- document.cookie = "utmcookie=" + utmcookie+';expires='+now.toGMTString()+';path=/';
- }
- // getCookie function
- function getCookie(cname) {
- var name = cname + "=";
- var ca = document.cookie.split(';');
- for(var i=0; i<ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1);
- if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
- }
- return "";
- }
- // make sure there aren't any existing cookies
- if( !getCookie("utmcookie").length > 0 ){
- // if url contains any utm parameters run code
- if(window.location.href.indexOf("utm") > -1){
- // slice parameters to return query string
- var QueryString = function () {
- var query_string = {};
- var query = window.location.search.substring(1);
- var vars = query.split("&");
- for (var i=0;i<vars.length;i++) {
- var pair = vars[i].split("=");
- if (typeof query_string[pair[0]] === "undefined") {
- query_string[pair[0]] = pair[1];
- } else if (typeof query_string[pair[0]] === "string") {
- var arr = [ query_string[pair[0]], pair[1] ];
- query_string[pair[0]] = arr;
- } else {
- query_string[pair[0]].push(pair[1]);
- }
- }
- return query_string;
- } ();
- // call each utm query string key value
- // establish identifier of each key value followed by that key value ( separated by "=")
- // separate each utm type by "|"
- var utmcookie = "utm_source=" + QueryString.utm_source + "|" + "utm_medium=" + QueryString.utm_medium + "|" + "utm_campaign=" + QueryString.utm_campaign + "|" + "utm_content=" + QueryString.utm_content + "|" + "utm_term=" + QueryString.utm_term + "|" + "utm_adgroup=" + QueryString.utm_adgroup;
- createCookie(utmcookie);
- }else{
- // if UTM isn't present extract source / medium by
- // Direct
- // Organic
- // Referral
- // list of all prominent Search Engines
- // can be found here http://antezeta.com/news/google-analytics-default-search-engines
- var searchEngines = new RegExp("(about|alice|aol|ask|search\.avg|isearch\.babylon|baidu|bing|searchh\.conduit|google|incredimail}lycos|mamma|msn|naver|search\.netscape|pchome|search-results|terra|yahoo|yam|yandex)\.com|biglobe\.ne\.jp|cnn\.com\/SEARCH\/|search\.comcast\.net|daum\.net|eniro\.se|go\.mail\.ru|goo\.ne\.jp|kvasir\.no|najdi\.org\.mk|online\.onetcenter\.org|ozu\.es|rakuten\.co\.jp|\.rambler\.ru|search\.centrum\.cz|seznam\.cz|startsiden\.no|search\.virgilio\.it|voila\.fr|wp\.pl", "g");
- if( document.referrer.match( searchEngines ) ){
- var utmcookie = "utm_source=" + document.referrer + "|" + "utm_medium=" + "organic" + "|" + "utm_campaign=" + "undefined" + "|" + "utm_content=" + "undefined" + "|" + "utm_term=" + "undefined" + "|" + "utm_adgroup=" + "undefined";
- createCookie(utmcookie);
- }else if( document.referrer.length > 0 && !document.referrer.match(/safestep/g) ){
- var utmcookie = "utm_source=" + document.referrer + "|" + "utm_medium=" + "referral" + "|" + "utm_campaign=" + "undefined" + "|" + "utm_content=" + "undefined" + "|" + "utm_term=" + "undefined" + "|" + "utm_adgroup=" + "undefined";
- createCookie(utmcookie);
- }else{
- var utmcookie = "utm_source=" + "direct" + "|" + "utm_medium=" + "none" + "|" + "utm_campaign=" + "undefined" + "|" + "utm_content=" + "undefined" + "|" + "utm_term=" + "undefined" + "|" + "utm_adgroup=" + "undefined";
- createCookie(utmcookie);
- }
- }
- }
- if( sourceSelector != null || mediumSelector != null ){
- // FORM FILL
- // search for any utm cookies exist
- // add the cookie values into the form
- for (var i = sourceSelector.length - 1; i >= 0; i--) {
- // identify each form element by ID and store in variable
- var utm_source = sourceSelector[i].value;
- var utm_medium = mediumSelector[i].value;
- var utm_campaign = campaignSelector[i].value;
- var utm_content = contentSelector[i].value;
- var utm_term = termSelector[i].value;
- var utm_adgroup = adgroupSelector[i].value;
- // slice all cookies by cookie name
- if (document.cookie.indexOf("utmcookie") > -1 == true){
- // get utm cookie
- utmcookie = getCookie("utmcookie");
- // if utm cookie exists run code
- if(utmcookie != null == true){
- // individually slice each utm parameter[i] value from
- // the pipe delimiter and establish[i] value into the form per variable
- // if returned "undefined", do nothing
- // utm_source
- if( utm_source != null == true ){
- var source = utmcookie.split('|')[0].split('=').slice(1)
- if( source == "undefined" ){ stop() }else{ sourceSelector[i].value = source; }
- }
- // utm_medium
- if( utm_medium != null == true ){
- var medium = utmcookie.split('|')[1].split('=').slice(1)
- if( medium == "undefined" ){ stop() }else{ mediumSelector[i].value = medium; }
- }
- // utm_campaign
- if( utm_campaign != null == true ){
- var campaign = utmcookie.split('|')[2].split('=').slice(1)
- if( campaign == "undefined" ){ stop() }else{ campaignSelector[i].value = campaign; }
- }
- // utm_content
- if( utm_content != null == true ){
- var content = utmcookie.split('|')[3].split('=').slice(1)
- if( content == "undefined" ){ stop() }else{ contentSelector[i].value = content; }
- }
- // utm_term
- if( utm_term != null == true ){
- var term = utmcookie.split('|')[4].split('=').slice(1)
- if( term == "undefined" ){ stop() }else{ termSelector[i].value = term; }
- }
- // utm_adgroup
- if( utm_adgroup != null == true ){
- var adgroup = utmcookie.split('|')[5].split('=').slice(1)
- if( adgroup == "undefined" ){ stop() }else{ adgroupSelector[i].value = adgroup; }
- }
- }
- }
- }
- }
- }
- }
- var sourceSelector = document.querySelectorAll('[name*="txtutm_source"]');
- var mediumSelector = document.querySelectorAll('[name*="txtutm_medium"]');
- var campaignSelector = document.querySelectorAll('[name*="txtutm_campaign"]');
- var contentSelector = document.querySelectorAll('[name*="txtutm_content"]');
- var termSelector = document.querySelectorAll('[name*="txtutm_term"]');
- var adgroupSelector = document.querySelectorAll('[name*="txtutm_adgroup"]');
- UTMParserFormFill(
- sourceSelector,
- mediumSelector,
- campaignSelector,
- contentSelector,
- termSelector,
- adgroupSelector )
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement