Access the URL query string in Visits Web Page triggers

As you may know, there's no {{trigger.query string}} token, which can be frustrating since Marketo usually (though not always) considers a “web page” to include only the hostname, pathname, and (somewhat curiously) the hash. That is, {{trigger.web page}} omits the query string, though that may be the part you're the most interested in.

Even if you're not dealing with the token, if you want to match Visits Web Page on two independent parts of the query string you're out of luck: you can only have one Constraint directly on Query String.

But run this little snippet before you init Munchkin:

<script>
if ( document.location.search &&
  history.replaceState && 
  !(history.state && history.state.searchCopied) ) {
  var oldLoc = document.createElement('a'),
      newLoc = document.createElement('a');
  
  newLoc.href = oldLoc.href = document.location.href;
  (/\/$/).test(newLoc.pathname) || ( newLoc.pathname += '/' );
  newLoc.pathname += '$query:' + newLoc.search.substring(1) + '/';
  history.replaceState({searchCopied:true}, null, newLoc.href);
  window.addEventListener('beforeunload',function(e){
    history.replaceState({searchCopied:false}, null, oldLoc.href);
  });
}
</script>

Now, we've temporarily copied the query string into a new path segment (appending to the original path) so {{trigger.web page}} will reflect the original query string as well as the host/pathname/hash!

In other words, if someone goes to this page…

http://pages.example.com/lp.html?param1=value1&param2=value2&param3

… the resulting {{trigger.web page}} will be…

pages.example.com/lp.html/$query:param1=value1&param2=value2&param3/

… greatly enhancing its usability.

That's a thing?

Sure, having data after the so-called “filename” in a URL is totally okay. It's only by convention that we think /the/last/part/is/always/the/filename. The filename (as far as there even is one, as dynamic websites often have no underlying file like you'd see in Finder/Explorer) could be further up in the hierarchy. Then the rest of the path can be used by the file or file-like handler. I prepended the term query for clarity, but this isn't really necessary.

You could append the query data to the URL #hash instead of the pathname:

// (/\/$/).test(newLoc.pathname) || ( newLoc.pathname += '/' );
// newLoc.pathname += '$query:' + newLoc.search.substring(1) + '/';
newLoc.hash += '$query:' + newLoc.search.substring(1) + '#';

And the Web Page seen by Marketo will now be:

pages.example.com/lp.html#$query:param1=value1&param2=value2&param3#

Also, we're just changing the URL in the browser so Munchkin logs what we want. The server never sees it.

Caveats

Won't work in IE 8 or 9. So be it. You can get the same effect w/a full page refresh in those browsers, but probably isn't worth it.