SOLVED

Keep/Pass persistent querystring in url

Go to solution
JD_Nelson
Level 10 - Community Advisor

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Keep/Pass persistent querystring in url

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

 

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Keep/Pass persistent querystring in url

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

 

JD_Nelson
Level 10 - Community Advisor

Re: Keep/Pass persistent querystring in url

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