Keep/Pass persistent querystring in url | Community
Skip to main content
Community Manager
November 4, 2020
Solved

Keep/Pass persistent querystring in url

  • November 4, 2020
  • 1 reply
  • 2157 views

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.
Best answer by SanfordWhiteman

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); } })();

 

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
November 4, 2020

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); } })();

 

JDNelson1Author
Community Manager
November 4, 2020

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