2 Replies Latest reply on Aug 15, 2017 10:57 PM by Sanford Whiteman

    Adding a cookie to a marketo landing page

    Jen Garcia

      I  need to add a cookie to a marketo landing page. I have the script but I am not sure how to call to it on successful form submit. Here is the script:

       

      function writeCookie (cName, vals) {

              var cVal = vals.FirstName+'|' +

                  vals.LastName +'|' +

                  vals.Email    +'|' +

                  vals.Company  +'|' +

                  vals.Role__c    +'|' +

                  vals.Phone  +'|' +

                  vals.Country  +'|' +

                  vals.State;

       

              var tos = ['Double_Opt_in_Compliant__c', 'doubleOptinCompliantCanada', 'termsofService'];

              $.each(tos, function(i, v) {

                  if (vals[v]) {

                      cVal += '|' + v + '|' + vals[v];

                      return false;

                  }

                  if (i == tos.length - 1) {

                      cVal += '||';

                  }

              });

              var expire = new Date();

              expire.setDate(expire.getDate() + 30);

              document.cookie = cName + "=" + encodeURIComponent(cVal) + ";expires=" + expire.toString() + "; path=/";

          }

       

      the cookie is "couchbase-download"

       

      How can I implement this?

       

       

        • Re: Adding a cookie to a marketo landing page
          Sanford Whiteman

          Put it in an onSuccess event listener:

           

          MktoForms2.whenReady(function(form){
            form.onSuccess(function(vals,tyURL){
              writeCookie('couchbase-download',vals);
            });
          });
          

           

          You may know this, but anyone who wants to write the cookie without submitting a form would have an easy time of it (since the logic is in the page) but for good-faith protection it's OK.

           

          More important, the cookie is not writing to the parent private suffix (.example.com) but to the full hostname (info.example.com) so as of right now this cookie will not be readable on your main corporate domain.

           

          P.S. The writeCookie code that assembles the value is way too wordy for what needs to be done, but I made myself leave it alone.

            • Re: Adding a cookie to a marketo landing page
              Sanford Whiteman

              P.S. The writeCookie code that assembles the value is way too wordy for what needs to be done, but I made myself leave it alone.

              OK, I couldn't look at it anymore without rewriting it.

               

              Something in this vein is better for maintainability:

               

              var fields = {
                core: [ "FirstName", "LastName", "Email", "Company", "Role__c", "Phone", "Country", "State"],
                tos: [ "Double_Opt_in_Compliant__c", "doubleOptinCompliantCanada", "termsofService" ]
              };
              
              // all core field values or string "undefined"
              var coreValues = fields.core.map(function(fieldName) {
                return vals[fieldName] || "undefined";
              });
              
              // first filled ToS field name and its value
              var tosValues = fields.tos.reverse().reduce(function(lastFound, fieldName) {
                return vals[fieldName] ? [fieldName,vals[fieldName]] : "|";
              }, null);
              
              // all together now
              var allValues = coreValues.concat(tosValues).join("|");