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

    • New field in Zoho Forms: Validate structured data with the Regex field

      Have you ever received form submissions where the information looked almost correct but wasn't quite in the format you expected? Maybe an employee ID was missing a character. A PAN number was entered in lowercase. A license number had an extra space.
    • Password-Protected PDFs in Zoho Forms

      Hello form builders, PDFs generated from your form submissions travel through email, across devices, and between hands you didn't intend. For a restaurant's event inquiry, that's fine. But for a signed legal agreement, a loan application, or personal
    • Introducing Spotlight Forms

      Hey form builders! If someone opens your form, sees the wall of fields ahead, and quietly closes the tab. It may not be because the questions were hard. It could be because the experience felt like too much. Which is why we have now introduced a new form
    • Access millions of Unsplash photos directly from the Theme Builder

      Hello form builders, We're excited to introduce a new enhancement to the Zoho Forms Theme Builder. You can now browse and apply millions of high-quality images from Unsplash without leaving the Theme Builder. There's no need to download images, switch
    • Introducing the Yes/No field: Binary decisions made beautiful

      Greetings, form architects! What would you do when you need a simple yes/no answer on your form? Normally, you add a Radio field. Type Yes. Type No. Until now. The new Yes/No field is purpose-built for binary decisions. It is preconfigured, visually consistent,
    • Recent Topics

    • Backup to external location (Dropbox etc)

      Dear Zoho Team, I've set the CRM Backup schedule to once a month, which when complete sends me an email. Of course I then need to manually login and download the file. Nice but VERY 20th century and relies on us humans not to fail. Can you please provide
    • Cloud backup for CRM

      This is an idea / feature request for ability to periodically export CRM backup to an external cloud provider, similar to Zoho Vault: https://www.zoho.com/vault/data-backup.html Alternatively I would help being able to access manual data backup programatically
    • Introducing throw statements in Deluge

      Hello everyone, We're introducing a powerful addition to Deluge that gives you more precise control over error handling in your scripts. Whether you're calling an external API, validating user input, or enforcing a business rule, there are moments when
    • WhatsApp Coexistence

      Hello, please implement WhatsApp Coexistence, we need to track staff whatsapp chats on their PC and they need to be able to use WhatsApp on their mobile in around the office and warehouse, as we video call customers and send them product pictures. Thank
    • Zoho Creator to Zoho Writer for prefilled documents...

      In response to the question about connecting Zoho Creator to Zoho Writer for prefilled documents, I wanted to share a working implementation that demonstrates how to use the record_id parameter with the Zoho Writer Merge API. This allows Writer to automatically
    • Upload a video to embed into an article

      How can we upload a video to embed into the article...? We can upload images but this only seems to support image files. And the video option seems to just support YouTube, Vimeo and DailyMotion which is no good, we need to create our own videos and upload
    • PAN - Aadhar Link Status

      Can Zohobooks also get latest PAN-Aadhar Linking Status from Income Tax Portal ?
    • LinkedIn RSC is now live in Zoho Recruit

      LinkedIn Recruiter System Connect (RSC) is here. Your Zoho Recruit data (candidates, jobs, notes, stage updates, resume attachments, and more) now syncs with LinkedIn in real time. Note: LinkedIn RSC is included with your LinkedIn Recruiter Corporate
    • Zoho Writer for Android - Headings

      Hello Zoho team, I have created structured doc with headings in writer web version on pc. Then I have opened it in my phone. Write is crashing while used function: menu "three dots", Document Navigation, Headings. Zoho Writer for Android 6.1.4 Samsung
    • The in between line spacing management

      In the Microsoft Word, when we write something there is a tool from where we can manage the line spacing. Here in writer app in mobile it is by default 1.5. I request Zoho Team to give the option from where I can control Line Spacing, Remove space between
    • Zoho Books | Product updates | September 2026

      Hello Partners, We've rolled out new updates to Zoho Books. From a brand-new Nigeria edition expanding our presence on the African continent to the new Super Admin privilege, here's everything that's new this month. Nigeria Edition Now Live [Nigeria Edition]
    • Zoho Books | Product updates | August 2026

      Hello users, July has been an exciting month for Zoho Books! This month, we're excited to introduce HTML PDF Templates, Placeholders as Pills, expanded approval workflows for Sales Returns and Journals, and significant compliance updates across the India,
    • Pulling data from lookup fields

      If I try to pull in CRM data from a lookup field, it only shows a number and not the actual name. Is there a way to get around this? It impossible to do reports using lookup field data.
    • CRM x WorkDrive: We're rolling out the WorkDrive-powered file storage experience for existing users

      Release plan: Gradual rollout to customers without file storage add-ons, in this order: 1. Standalone CRM 2. CRM Plus and Zoho One DCs: All | Editions: All Available now for: - Standalone CRM accounts in Free and Standard editions without file storage
    • Marketing Tip #49: Set up Meta Pixel and turn store visitors into repeat buyers

      Most of your store visitors don't buy on their first visit. They browse, get distracted, and leave. Without a way to reach them again, that traffic is gone forever. Meta Pixel changes that. What is Meta Pixel? Meta Pixel (previously called Facebook Pixel)
    • unable to join meeting due to speaker issue

      unable to join meeting due to speaker issue
    • Unlock smarter scheduling with AI in Zoho Bookings

      Hello everyone! Scheduling touches more parts of a business than we often realize. Teams need to coordinate. Workflows need to move. Business apps need to connect. And with AI, the way we interact with scheduling is changing too. Join our upcoming live
    • Zoho CRM Layout Rules: Nine New Actions, Profile-Based Execution, and Interactive Preview

      Hello everyone, Availability: This feature is now available for all users. We’re excited to announce powerful new enhancements to Layout Rules in Zoho CRM - a feature built to guide our teams through form-filling by showing the right information to the
    • I want to delete payment detail of my cards from the system.

      I want to delete payment detail of my cards from the system.
    • Urgent: Outgoing Emails Delayed Across All Zoho Desk Departments

      Dear Zoho Support, We have started receiving delivery delay notifications across all our Zoho Desk departments. Error details: Error: 451 4.7.500 Server busy Action: Delayed Receiving system: Microsoft 365 / Exchange Online Affected Zoho sending IPs identified
    • Les meilleures pratiques d’email marketing pour booster les ouvertures, les clics et les conversions

      L’email marketing connaît aujourd’hui des changements profonds. Gmail, Yahoo et Microsoft ont notamment renforcé leurs exigences en matière d’authentification, ce qui rend la délivrabilité plus complexe et exigeante. Dans le même temps, les filtres basés
    • Header Line in Quote product rows

      I believe this is an essential feature that Zoho should provide us, when adding products we want the ability to add titles for our product, in quotes: The Solution/Category Title Product1 Product2..... and then it would show the sub-total of that title 
    • add pdf bills

      as zoho scan is to high cost,and claude and others as costing low , why u are not costing less than apps
    • Error with bank feed activaiton (Global edition)

      Hi there, We've started using Zoho Books in NZ and have been struggling to activate bank feed from BNZ. Althouhg following the instruction to active bank feed, an error message appears every time as attached. Plase advise how to resolve this matter. Thank
    • prefill_data for Creator + Merge & Sign

      Hello, Is there an option to use prefill_data for signer fields from creator? I see the option for CRM but it will not work with creator? I have tried passing the value in the signer data, and that also does not work. Alternatively, is there a way to
    • Kaizen #258 - Getting Started with zoho-crm SKILL.md

      Howdy tech wizards, Welcome to a fresh week of Kaizen. This week, we are taking a look at the zoho-crm SKILL.md, an Agent Skill designed to help AI coding agents work with Zoho CRM’s developer capabilities. What is zoho-crm SKILL.md? The zoho-crm skill
    • Support Required for Integrating Zoho SalesIQ in Flutter Web

      Hi Team, I have developed a Flutter application and I am currently using the salesiq_mobilisten: ^6.5.4 package. As per the package documentation ( https://pub.dev/packages/salesiq_mobilisten ), it supports only Android and iOS, and it is working fine
    • Books is extremely slow again today !

      Everything is running slowly even with 500mb connection speed
    • Automate Credit Card Surcharge

      Is there a way to create an automation that will add a 3.0% credit card surcharge to a subscription whenever a customer pays via credit card?
    • How to charge Convenience fee OR payment gateway charges to the end client who is paying the invoice?

      Hello, I am creating this topic after having discussions with various sets of users and have understood that with people moving more and more to digital payments, it is important for the client to enable the "Convenience fee" kind of scenario. I have
    • How to add a fee based on payment method

      I currently accept both ACH payments and credit card. I want to add a 3% fee to a number of my subscription plans if the customer chooses to use a credit card (and not ACH/checking). How do I do this? Is there an in-built feature, or would i have to create
    • Customer Statement Showing Incorrect Negative Balance Due to Unused Advance

      Dear Zoho Support Team, We are facing an issue with the Customer Statement in Zoho Invoice related to customer advances/mobilization advances. The issue is that when a customer has made an advance payment that has not yet been fully utilized against invoices,
    • ICICI Bank integration bulk payments to multiple vendors

      Hi Team, I would like to know why ICICI Bank integration doesn't allow bulk payments to multiple vendors. Other banks such as HSBC, Yes Bank and Axis Bank allows zoho integration to make payments to multiple vendors at a time
    • Zoho Inventory - Profit Margin Feature - Allow Users to Set Default Cost Price Calculation Method

      Hi Zoho Inventory Team, I'm working with an automotive parts distributor using Zoho Inventory. They have expressed frustration about having to change the Cost Price Method on every line item they add from the defult "Inventory Valuation Method" to "Item
    • Deprecation Notice: OpenAI Assistants API will be shut down on August 26, 2026

      I recieved this email from openAI what does it means for us that are using the integration and what should we do? Earlier this year, we shared our plan to deprecate the Assistants API once the Responses API reached feature parity. With the launch of Conversations,
    • Payment for product renewal

      Good morning, I am requesting your kind assistance to renew my subscription because when I try to make the payment I get this way
    • Calendar Year View?

      Is there a way I can view the calendar in year view? Maybe create a page with a view like this?
    • Razorpay + Zoho Billing + Zoho Books Integration

      Please help us set up this integration.
    • Proyecto SII

      Buenas tardes amigos de zoho. en mi empresa vamos a implementar el proyecto de emisión de facturas SII pero para poder hacerlo hay que subir un xml, estoy intentando automatizar este proceso pero no se como convertir la factura que retorna books a xml
    • Automate Workflows at the Subform Level

      Hello, Enterprise Support Community! We are excited to introduce an enhancement to Workflow Rules that gives you more control over automation within your records. Previously, Workflow Rules could only be configured at the module level, which meant changes
    • Next Page