Check if the current pageview is associated with a Marketo lead (without using the ol’ hidden KV HTML trick)

Knowing if the current pageview is associated with a known lead vs. anonymous is key to a lot of cool personalization.

For example, you can build the equivalent of “If Known Visitor, show Custom HTML” with a non-Marketo form. Or show instant download links if someone is associated (since clicks will be logged by Munchkin) vs. a modal form if they’re anonymous.

💡
Notice I said “pageview” rather than “session” or “cookie” to be precise. A page will render in the context of a known record if it has the mkt_tok param from an email click, even if cookies are completely disabled. Of course, that type of association won’t travel to the next page, but the current page is associated.

The old way

The typical way to do this is create a dedicated “association checker” Marketo form with Known Visitor HTML enabled.

  • in the KV HTML block, have a <script> that runs your “if associated” code
  • in the regular form, add a Rich Text with a <script> that runs your “if anonymous” code

When you load this form into a hidden <form style="display:none;">, either the KV HTML JS or the form JS will run, so it serves as an implicit detection mechanism.

But you’ve wasted resources loading the Forms 2.0 library, the XDFrame, and the form itself, just to use the built-in known lead lookup. Maybe it just takes an extra second, but it feels sloppy.

The new way

What if instead you hit getKnownLead directly? Enter a little function that does just that, checkMktoAssociation().

Get the code

Grab checkMktoAssociation() from here and rehost it in Design Studio:

Usage

checkMktoAssocation(formHostname,munchkinId,useCache)

Parameters

  • formHostname (string, required)
    Marketo LP domain or domain alias (do not include the protocol).
  • munchkinId (string, required)
    You know this one. 😏
  • useCache (boolean, optional, default true)
    Use session storage to remember the last _mkto_trk that was associated. Since cookies can’t go from associated back to anonymous, it’s harmless and ups performance.

Return value

Promise that either

  • resolves with true/false (associated/anonymous)
  • rejects with an Error if there’s some network-level or config-level problem (like entering a nonexistent munchkinId)

Examples

Just run, handle errors elsewhere:

    checkMktoAssociation("lp.example.com", "123-ABC-456")
    .then( isAssociated => {    
        if(isAssociated){
          // true, do associated stuff
        } else {
          // false, do anonymous stuff
        } 
    });

Catch errors and map them to undefined:

    checkMktoAssociation("lp.example.com", "123-ABC-456")
    .catch( error => {
        console.error(error);
        return undefined;
    })
    .then( isAssociated => {    
        if(isAssociated){
          // true, do associated stuff
        } else {
          // false or undefined, do anonymous stuff
        } 
    });

If you’re into async/await (meh):

    let isAssociated;
    try {
       isAssociated = await checkMktoAssociation("lp.example.com","123-ABC-456");
    } catch(error) {
       console.error(error);
       isAssociated = undefined;
    }