10 Replies Latest reply on Dec 10, 2018 12:36 PM by Craig Boren

    Include a Marketo field in custom HTML for a known visitor

    Craig Boren

      I'm trying to include a Marketo field in a custom HTML message for known visitors. However, when I test the form out, I am seeing a "Uncaught TypeError: Cannot read property 'name' of undefined" in the Dev Console (referring to this field). Because of this, on form submit, the page just reloads instead of submitting the form. See this for an example:



        • Re: Include a Marketo field in custom HTML for a known visitor
          Sanford Whiteman

          A lot of what you're doing here doesn't really make sense.


          1st: You don't have to include remote <script>s in the KV HTML. Since this is a Marketo-hosted LP fully under your control, just put them in the page. In fact both of the scripts you have in the KV HTML are also in the page, so you're just wasting resources.


          2nd: No need to wait for DOMContentLoaded to check cookies. If the cookie was set on earlier pageview it's available to read immediately. (But see #4, you don't want to do this in the KV HTML only.)


          3rd: If you want hidden fields to show up in the KV HTML form submission you must use the Forms API addHiddenFields method. Don't just add them to the rich text area.


          4th: To populate a container in the KV HTML with a Marketo {{lead.token}}, create a placeholder <span> for it (i.e. with an identifiable data-attr or class).  Then in your KV HTML have only a one-line script that calls an external function to signify that you're in KV HTML mode. That external function, not stored within KV HTML, does everything else.

            • Re: Include a Marketo field in custom HTML for a known visitor
              Sanford Whiteman

              ... actually, you don't need any code in the KV HTML at all. I forgot my own recommended practice in this regard.


              Add a whenReady listener and check for an HTML element known to only exist in KV HTML. The easiest example in your case is the Not You? link.


                var formEl = form.getFormElem()[0],
                    kvSentinel = formEl.querySelector(".mktoNotYou");    
                    if (kvSentinel) {
                      /* KV HTML is in effect, do stuff */
            • Re: Include a Marketo field in custom HTML for a known visitor
              Craig Boren

              Thanks Sanford for you response! First off, what does KV HTML stand for? I'm assuming it means the HTML dialog in the form editor?


              Anyway, I totally get the notion of not needing to include remote scripts in the HTML input. In my efforts to try and get the form to work, I just overlooked that aspect. Secondly, I'm really not worried about the cookie part not working. That was just a workaround I was using since my first approach wasn't working. Thirdly, this is good to know! I didn't know how to add Hidden Fields, so this will help. Lastly, I'm not sure what you're referring to here. You can probably guess that I'm not too familiar with the Marketo forms 2.0 API. That said, I really appreciate your help so far!



              Really, all I'm trying to do is, on a return visit, the user will see the following message:



              How do I include the "Click here to receive ongoing news, information, and offers......" checkbox field? (this is a Marketo field that we sync with Salesforce to help track a user's communication preferences)