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?







    • Sticky Posts

    • DKIM Now Mandatory - Changes to Zoho Forms Email Policies

      Hello Zoho Forms Users, This post is to inform you about an important update regarding the authentication of all email domains in your Zoho Forms account. This year, we are doubling down on our commitment to deliver a secure, seamless, and empowering
    • Cloudflare Turnstile is now available in Zoho Forms!

      Hello form builders! We have added a new layer of protection to help you keep your forms free from bots. Instead of forcing users to prove they are human, Cloudflare Turnstile quietly checks browser signals in the background. Your real users glide through,
    • This festive season, offer discounts with coupon code support in Stripe Checkout

      Hello form builders! It’s the festive season, the perfect time to spread joy and great deals! Now, with Zoho Forms’ latest enhancement for Stripe Checkout, you can do exactly that with coupon codes! Your payment forms integrated with Stripe Checkout can
    • New in Zoho Forms: Google reCAPTCHA v3 for smarter spam protection

      Hello form builders, Spam submissions are one of the biggest challenges when you share your forms online. They not only clutter your data but can also waste valuable time. To help you combat this without making life harder for genuine respondents, we’re
    • New in Zoho Forms: Inline OTP Verification

      Hello form builders, We are excited to announce the launch of Inline OTP Verification in Zoho Forms, a smarter way to ensure the authenticity of the contact details you collect. Until now, OTP Verification in Zoho Forms worked as a pre-access step: respondents
    • Recent Topics

    • Zoho Writer's WordPress extensions

      Hey Zoho Writer users! Say goodbye to all your WordPress content publishing woes with Zoho Writer's WordPress extensions. Publish content with all your formatting and images, republish content when you update a document, and more—from a single window
    • Time-saving table hacks

      Hey Zoho Writer Community, Do you find yourself using a lot of tables in your documents? We're here to share some of our time-saving hacks that will help you work more efficiently, organize your data, and make your documents look neat and professional.
    • Automating document approval and signing with Zoho Writer and Zoho Sign

      Hey Zoho Writer Community! Here's another automation tip to make your processes more efficient! Question: Can I send a document for client approval first, then automatically send it for signing with Zoho Sign if they approve? Since it's the same person
    • Customization hacks in Zoho Writer - Part 2

      Hey community, We're back with some more tricks to personalize your documents, save time, and get in the zone when you work in Writer. Check out part 1 of this post if you haven't already. Let's dive right in! Document ruler units Imagine you're creating
    • Simplify your tax calculations with Zoho Writer

      Hello Zoho Writer Community! Tax season can be stressful, but with Zoho Writer, managing your income tax calculations becomes straightforward and efficient. Here’s an example of how you can use the tables and formulae of Zoho Writer to calculate income
    • Enhance document navigation with headings and TOC

      Hey Zoho Writer Community! We're back with some useful features in Zoho Writer that can simplify your document creation and navigation process. Let's dive right in! Check out our video on how to make the most of Zoho Writer's heading and table of contents
    • Use and download in PDF format of Zoho wirter Merge template using deluge

      Hello Zoho Developers. Here is some information about Zoho Writer. Writer is not just another online word processor, it's a powerful tool for editing, collaboration, and publishing. Even with its wide range of features, Writer's pared-down user interface
    • Customization hacks in Zoho Writer - Part 3

      Hello everyone, Welcome back to Part 3 of our customization tips in Zoho Writer! In this third installment, we'll be diving into some essential customization settings that can enhance your document creation experience. Sender email address in mail merge
    • Daytime saving timezones messing up writer pdf

      Hi, I need help for something I can't figure out. I created a Form to collect data and it is set up with my current Daylight Saving Time (GMT-3). This form is used to generate a contract (pdf Writer) with dates from an event that is being held in 4 months
    • Issue with locked content in Writer

      Hi, I have seen the documentation which outlines how to lock specific content within a Writer document so that it can't be modified by collaborators, but I have come across an issue. When the editor locks a paragraph for example, then a collaborator can't
    • Deprecation of certain URL patterns for published Zoho Writer documents

      Hi Zoho Writer users! We'd like to let you know that we have deprecated certain URL patterns for published and embedded documents in Zoho Writer due to security reasons. If the published or embedded documents are in any of these URL patterns, then their
    • [Announcement] Enhanced Merge APIs of Zoho Writer for automating document generation using Zoho Apps data

      Hi users, With the record_id parameter in Zoho Writer's Merge APIs, fields related to the given record id is automatically fetched from the Zoho app. You don't have to spend time manually mapping the records and fields in the Deluge code. Here is a sample
    • [Important announcement] Removal of Zoho Writer's DocuFiller Add-on

      Hi users, Effective 21st Feb. 2025, we will be removing the DocuFiller add-on for Zoho Writer's Fillable Templates. From that point on, the add-on's functionalities, such as sharing or publishing fillable forms for data collection and receiving submissions
    • Using Mail Merge Template to Print Documents with One Subform Record's Fields per Document

      Hello, We have a Mail Merge template created in Zoho Writer which is not able to perform the functionality which is currently required to automate the documentation task portion of our process. The CRM module we are primarily using is based on a "Loans"
    • Zoho Writer - Option to Export as .zdoc format

      I've noticed that it's not possible to export a Zoho Writer Document in the .zdoc format. Isn't zdoc, Zoho Writer's own format? My use case is that I sometimes need to create quite complex documents with floating elements, which sometimes need to become
    • Problem with Writer and Workdrive

      Hi team, I’m the super admin for our Zoho One org. WorkDrive is active, and Zoho Docs is deprecated for our org. However, Zoho Writer cannot connect to WorkDrive at all — we’ve cleared cache, tried incognito, and restarted several times. I was able to
    • Set to Review for all

      We are testing the use of Writer as part of an internal review process for statement of work documents and have found that when the document is changed from Compose to Review by one person, that is not reflected for all others who view the document. Is
    • I’ve noticed that Zoho Sheet currently doesn’t have a feature similar to the QUERY formula in Google Sheets or Power Query in Microsoft Excel.

      These tools are extremely helpful for: Filtering and extracting data using simple SQL-like queries Combining or transforming data from multiple sheets or tables Creating dynamic reports without using complex formulas Having a Query-like function in Zoho
    • Shortcut to fill a range of cells

      Good evening: I'm writing because I haven't been able to find a feature that allows you to select a range of cells, type in one of them, and then use a key combination to type in all of them. In Excel, the keyboard shortcut is Ctrl+Enter. I haven't found
    • stock

      bom/bse : stock details or price =STOCK(C14;"price") not showing issue is #N/A! kindly resolve this problem
    • SOME FEATURES ARE NOT IN THE ZOHO SHEET IN COMPARISION TO ZOHO SHEET

      TO ZOHO sir/maam with due to respect i want to say that i am using ZOHO tool which is spreadsheet i want to say that some features are not there in zoho sheet as comparison to MS EXCEL like advance filter and other Features which should be there in ZOHO
    • Zoho sheet for desktop

      Hi is zoho sheets available for desktop version for windows
    • Slicers are now available in Zoho Sheet—filter your data interactively

      At Zoho Sheet, we diligently track user requests and feedback. In line with this, based on extensive user requests, we've integrated Slicers to pivot tables and are delighted to announce its release. Slicers are interactive visual filters that have add,
    • Tip #18: 6 Trendlines and when to use them in your spreadsheet data?

      Charts are a great tool for visualizing and interpreting large chunks of data in spreadsheets. Zoho Sheet offers you 35+ chart options, along with AI-powered chart recommendations based on the data set you select. There are various chart elements that
    • Feature enhancement: Highlight rows based on a cell value

      Hello Sheet users, We're excited to announce a new feature enhacement, shaped directly by your valuable feedback! As you might know, conditional formatting is a great tool for anyone dealing with large data sets. Previously, if you’ve ever wanted to draw
    • Perform customized calculations with the new LAMBDA functions!

      We released the LAMBDA function in Zoho Sheet in 2021, enabling customized calculations inside predefined functions. Now, we're extending LAMBDA's capabilities with six new functions. Each of these functions applies LAMBDA to the given input, performs
    • No chat option

      Chat option is not supported.
    • Tip #17: Easily share spreadsheets with non-Zoho account users

      With efficient, collaboration-friendly options, Zoho Sheet is a popular choice among users. Sheet's external share links help you collaborate more effectively by sharing your spreadsheets with anyone outside your organization, including users who do not
    • Work seamlessly with Sheet's shortcuts without affecting the browser

      Navigating your worksheets and performing specific actions should be a quick and easy process. That's where keyboard shortcuts come in. Shortcut keys help you accomplish tasks more efficiently, eliminating the need to move away from the keyboard. What
    • Zoho Sheet for iPad: Improved UI, external keyboard support, and more!

      The portability and capabilities of the iPad have made it an essential device in the business world. With this in mind, we had launched an exclusive Zoho Sheet app for iPad, with device-specific improvements and enhanced usability. Now, we're improving
    • Updated font library: New font styles and custom font options in Zoho Sheet

      Zoho Sheet's font library now supports 500+ font styles in 60+ languages! The updated font library is stacked with new font styles, and some of the previously available font styles have been replaced with equivalent options. There are two ways you can
    • The new Zoho Sheet for Android: Seamless UI and advanced features

      At Zoho Sheet, we know mobile apps have become the go-to platforms for creating, sharing, and storing information. And we understand the importance of an efficient and seamless app experience. We've been working hard on improving the overall user experience
    • Zia in Zoho Sheet now makes suggestions on conditional formats, picklists, and checkboxes

      Zia, Zoho's popular AI assistant, has been helping users succeed across Zoho applications since inception. In Zoho Sheet, Zia helps users efficiently build reports and analyze data with recommendations about data cleaning, charts, and pivot table. Also,
    • Introducing Data Bars: Graphically represent changes in data within the cells

      Conditional formatting has helped millions of spreadsheet users analyze and highlight their data more efficiently. In addition to the classic rules, color scales, and icon sets available in Zoho Sheet, you can now apply Data Bars, a convenient method
    • VBA attached to button no longer works on external shared sheet

      Hi - we have a Sheet that our clients use, which requires them to press a button once they've entered data. The button then triggers a flow via a webhook. The button works fine when editing within Zoho Sheet - however when shared externally it no longer
    • can I use zoho sheet for survey?

      Hi I wanna use zoho sheet for my ARY to know consumer better so i wanna have permission to do so
    • How to Avoid Impacting Other Users When Hiding Columns in Zoho Sheet

      Hi Team, We’re experiencing a challenge with the column-hiding feature on Zoho Sheet during collaborative sessions. When one user hides a column, it becomes hidden for all users working on the file, which disrupts others' workflows. In comparison, Excel
    • Tip #19 - Create checkbox tracker in your spreadsheet

      Hello Sheet users! We are here with yet another tip to help you get the most out of Zoho Sheet. Spreadsheets can be used to handle a variety of tasks, but ever tried using checkboxes to track the progress of your action items dynamically? Here's a sample
    • New features released: Data from Picture for Web, Pattern Fill, and Translation in Zoho Sheet!

      Hello Sheet users, In 2024, the Zoho Sheet team focused on research and development to bring new features that add to functionalities like productivity, data management, collaboration, and more. This year, we're all set to roll them out incrementally
    • What are the benefits of procurement software in a growing business setup?

      I’ve been exploring tools that can help automate purchasing and vendor-related tasks. I keep hearing about the benefits of procurement software, especially for businesses that are scaling. I want to understand how it helps in streamlining operations,
    • Next Page