Help with cookie code, utm variables are overwritten on every page load with new variables

Help with cookie code, utm variables are overwritten on every page load with new variables

Hi all,
My goal is to track the very first web page that a user visits, and store those utm values throughout all of the future visits until a lead form is submitted. 

That way, if a person is retargeted, I can still attribute the very first visit as the source of the customer.

Right now, I am using this code that is provided by the Forms tool:

  1. <script type="text/javascript"> 
  2. function ZFLead(){
  3. }
  4. ZFLead.utmPValObj = ZFLead.utmPValObj || {};

  5. ZFLead.utmPNameArr = new Array('utm_source','utm_medium','utm_campaign','utm_term','utm_content','utm_customVar1');

  6. ZFLead.prototype.zfutm_getLeadVal = function(pName){
  7. var qStr = '';
  8. try{
  9. qStr = window.top.location.search.substring(1);
  10. } catch (e){
  11. qStr = '';
  12. }
  13. var pNameTemp = pName + '=';
  14. var pValue = '';
  15. if ( typeof qStr !== "undefined" && qStr !== null && qStr.length > 0 ){
  16. var begin = qStr.indexOf(pNameTemp);
  17. if ( begin != -1 ){
  18. begin = begin + pNameTemp.length;
  19. end = qStr.indexOf( '&', begin );
  20. if ( end == -1 ){
  21. end = qStr.length;
  22. }
  23. pValue = decodeURIComponent(qStr.substring( begin, end ));
  24. }
  25. }
  26. if (pValue == undefined || pValue == ''){
  27. pValue = this.zfutm_gC(pName);
  28. }
  29. return pValue;
  30. };

  31. ZFLead.prototype.zfutm_sC = function( paramName,path,domain,secure ){
  32. var value = ZFLead.utmPValObj[paramName];
  33. if ( typeof value !== "undefined" && value !== null ){
  34. var cookieStr = paramName + "=" + encodeURIComponent( value );
  35. var exdate=new Date();
  36. exdate.setDate(exdate.getDate()+7);
  37. cookieStr += "; expires=" + exdate.toGMTString();
  38. cookieStr += "; path=/";
  39. if ( domain ) {
  40. cookieStr += "; domain=" + encodeURIComponent( domain );
  41. }
  42. if ( secure ) {
  43. cookieStr += "; secure";
  44. }
  45. document.cookie = cookieStr;
  46. }
  47. };

  48. ZFLead.prototype.zfutm_ini = function (){
  49. for (var i = 0; i < ZFLead.utmPNameArr.length ; i ++){
  50. var zf_pN = ZFLead.utmPNameArr[i];
  51. var zf_pV = this.zfutm_getLeadVal(zf_pN);
  52. if ( typeof zf_pV !== "undefined" && zf_pV !== null ) {
  53. ZFLead.utmPValObj[ zf_pN ] = zf_pV;
  54. }
  55. }
  56. for (var pkey in ZFLead.utmPValObj) {
  57. this.zfutm_sC(pkey);
  58. }
  59. };
  60. ZFLead.prototype.zfutm_gC = function( cookieName ){
  61. var cookieArr = document.cookie.split('; ');
  62. for ( var i = 0 ; i < cookieArr.length ; i ++ ){
  63. var cookieVals = cookieArr[i].split('=');
  64. if ( cookieVals[0] === cookieName && cookieVals[1] ) {
  65. return decodeURIComponent(cookieVals[1]);
  66. }
  67. }
  68. };
  69. ZFLead.prototype.zfutm_gC_enc = function( cookieName ){
  70. var cookieArr = document.cookie.split('; ');
  71. for ( var i = 0 ; i < cookieArr.length ; i ++ ){
  72. var cookieVals = cookieArr[i].split('=');
  73. if ( cookieVals[0] === cookieName && cookieVals[1] ) {
  74. return cookieVals[1];
  75. }
  76. }
  77. };
  78. ZFLead.prototype.zfutm_iframeSprt = function () {
  79. var zf_frame = document.getElementsByTagName("iframe");
  80. for(var i = 0; i < zf_frame.length; ++i){
  81. if((zf_frame[i].src).indexOf('formperma') > 0 ){
  82. var zf_src = zf_frame[i].src;
  83. for( var prmIdx = 0 ; prmIdx < ZFLead.utmPNameArr.length ; prmIdx ++ ) {
  84. var utmPm = ZFLead.utmPNameArr[ prmIdx ];
  85. var utmVal = this.zfutm_gC_enc( ZFLead.utmPNameArr[ prmIdx ] );
  86. if ( typeof utmVal !== "undefined" ) {
  87. if(zf_src.indexOf('?') > 0){
  88.                     zf_src = zf_src+'&'+utmPm+'='+utmVal;
  89. }else{
  90.     zf_src = zf_src+'?'+utmPm+'='+utmVal;
  91. }
  92. }
  93. }
  94. if ( zf_frame[i].src.length < zf_src.length ) {
  95. zf_frame[i].src = zf_src;
  96. }
  97. }
  98. }
  99. };
  100. ZFLead.prototype.zfutm_DHtmlSprt = function () {
  101. var zf_formsArr = document.forms;
  102. for ( var frmInd = 0 ; frmInd < zf_formsArr.length ; frmInd ++ ) {
  103. var zf_form_act = zf_formsArr[frmInd].action;
  104. if ( zf_form_act && zf_form_act.indexOf('formperma') > 0 ){
  105. for( var prmIdx = 0 ; prmIdx < ZFLead.utmPNameArr.length ; prmIdx ++ ) {
  106. var utmPm = ZFLead.utmPNameArr[ prmIdx ];
  107. var utmVal = this.zfutm_gC( ZFLead.utmPNameArr[ prmIdx ] );
  108. if ( typeof utmVal !== "undefined" ) {
  109. var fieldObj = zf_formsArr[frmInd][utmPm];
  110. if ( fieldObj ) {
  111. fieldObj.value = utmVal;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. };
  118. ZFLead.prototype.zfutm_jsEmbedSprt = function ( id ) {
  119. document.getElementById('zforms_iframe_id').removeAttribute("onload"); 
  120. var jsEmbdFrm = document.getElementById("zforms_iframe_id");
  121. var embdSrc = jsEmbdFrm.src;
  122.     for( var prmIdx = 0 ; prmIdx < ZFLead.utmPNameArr.length ; prmIdx ++ ) {
  123. var utmPm = ZFLead.utmPNameArr[ prmIdx ];
  124. var utmVal = this.zfutm_gC_enc( ZFLead.utmPNameArr[ prmIdx ] );
  125. if ( typeof utmVal !== "undefined" ) {
  126. if(embdSrc.indexOf('?') > 0){
  127.             embdSrc = embdSrc+'&'+utmPm+'='+utmVal;
  128. }else{
  129.     embdSrc = embdSrc+'?'+utmPm+'='+utmVal;
  130. }
  131. }
  132. }
  133. jsEmbdFrm.src = embdSrc;
  134. };
  135. var zfutm_zfLead = new ZFLead();
  136. zfutm_zfLead.zfutm_ini();
  137. if( document.readyState == "complete" ){
  138.     zfutm_zfLead.zfutm_iframeSprt();
  139. zfutm_zfLead.zfutm_DHtmlSprt();
  140. } else {
  141.   window.addEventListener('load', function (){
  142.         zfutm_zfLead.zfutm_iframeSprt();
  143. zfutm_zfLead.zfutm_DHtmlSprt();
  144.   }, false);
  145. }
  146. </script>

I tested this code, and I noticed that if it overwrites the existing utm variables if the user follows a new link with new utm variables.

I don't know javascript very well.

I am thinking that the solution to this would be to add 5 "first visit" custom variables in the form

Check if "first visit" have values, if no update, current utm values to "first visit", if yes update the standard utm values as well.

Is this the right idea?