Keep/Pass persistent querystring in url | Community
Skip to main content
Community Manager
Nov 4, 2020 12:02 AM UTC
Solved

Keep/Pass persistent querystring in url

I'm looking to keep url querystring parameters in a url string when passed from my 3rd party https page to my https marketo page before munchkin loads so I can trigger off the "Visits Web Page" trigger. Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Nov 4, 2020 6:25 PM UTC

You're in luck with this particular need because you don't actually need to parse the referrer's querystring (that is, parse its inner key=value pairs) at all, just chop out the querystring properly and plop it onto the end of the current URL.

 

For that we use a by-now typical pattern:

  • create a couple of built-in Location objects
  • use the the search property on any Location — that's the querystring, including the question mark
  • concatenate the search-es
  • replace the current URL without refreshing

Just make sure you run this code before loading Munchkin. On a Marketo LP, putting it in the head will suffice, since Munchkin loads at the end of the body.

 

(function () { var referrerLoc = document.createElement("a"), docLoc = document.createElement("a"); referrerLoc.href = document.referrer; // if referrer has a non-empty qs, transplant it to current doc if (referrerLoc.search.length > 1) { docLoc.href = document.location.href; docLoc.search += (docLoc.search ? "&" : "") + referrerLoc.search.substring(1); history.replaceState({}, undefined, docLoc.href); } })();

 

2 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Nov 4, 2020 6:25 PM UTC

You're in luck with this particular need because you don't actually need to parse the referrer's querystring (that is, parse its inner key=value pairs) at all, just chop out the querystring properly and plop it onto the end of the current URL.

 

For that we use a by-now typical pattern:

  • create a couple of built-in Location objects
  • use the the search property on any Location — that's the querystring, including the question mark
  • concatenate the search-es
  • replace the current URL without refreshing

Just make sure you run this code before loading Munchkin. On a Marketo LP, putting it in the head will suffice, since Munchkin loads at the end of the body.

 

(function () { var referrerLoc = document.createElement("a"), docLoc = document.createElement("a"); referrerLoc.href = document.referrer; // if referrer has a non-empty qs, transplant it to current doc if (referrerLoc.search.length > 1) { docLoc.href = document.location.href; docLoc.search += (docLoc.search ? "&" : "") + referrerLoc.search.substring(1); history.replaceState({}, undefined, docLoc.href); } })();

 

Translate
JDNelson1Author
Community Manager
Nov 4, 2020 8:12 PM UTC

plug and play -- perfection! Thanks Sanford!!!

Translate