5

So, I found this similar request, but the poster answered himself with going into details:

Persistent URL query string throughout the entire site?

Essentially, I have an event management website where I can edit html and javascript, but not php. I am trying to setup passing a discount code through a link to use for affiliate tracking. The system already allows for the code to be passed in a query in the form of:

https://www.example.com/reg/newreg.php?eventid=89335&discountcode=code

BUT

The caveat is that the system only allows that when linking the person directly into the registration process as shown above. Since no one is going to want to buy a ticket without seeing the event details, this is all but useless. The address of the homepage is in the form of:

https://www.example.com/home/89335

But when I try appending &discountcode=code to the address, the query string is lost upon clicking the registration link and going to the registration page.

Any suggestions on how to handle this?

Thanks!

Community
  • 1
  • 1
joshttbnew
  • 63
  • 1
  • 7

1 Answers1

3

You could try to parse all a-tags containing a href and update the href using javascript.

If you can use jQuery, you could do it that way (completely untested, so this could not work directly, but you should get the way to the solution :))

To get the url param, you could use the solution from here: https://stackoverflow.com/a/5158301/2753126

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

jQuery(document).ready(function ($) {
  var persistedQueryParam = getParameterByName('discountcode');

  if (persistedQueryParam && persistedQueryParam.length > 0) {
    $('a[href]').each(function () {
      var elem = $(this);
      var href = elem.attr('href');
      elem.attr('href', href + (href.indexOf('?') != -1 ? '&' : '?') + 'discountcode=' + persistedQueryParam);
    });
  }
});

Just replace the 'discountcode' strings with your real parameter name.

Hope that helps.

Best regards, Chris

Update: added querystring and '?' / '&' handling.

Update 2: changed from prop to attr.

Update 3: changed from 'param' name to 'discountcode'.

Uüdate 4: added length check @ persistedQueryParam and finally got the chance of testing the script :)

Community
  • 1
  • 1
Chris
  • 326
  • 1
  • 6
  • Hey Chris, thanks for the answer. I can use jquery, but I know only the tiniest bit of it. Can you clarify the answer some when you have a chance? I can see it looks like are targeting all links on the page and adding persistedQueryParam, but I'm not sure how to get the right value for persistedQueryParam in the first place. – joshttbnew Apr 12 '14 at 20:31
  • also, since I only have a couple of links where this needs to happen, could i target by class? something like: $('a.persist-link').each(function () ? – joshttbnew Apr 12 '14 at 20:34
  • Updated. Yes, if you can narrow it down by using a css-class, you should do that for sure. – Chris Apr 12 '14 at 20:52
  • Is there any reason why you're using `.prop()` over `.attr()`? The former is typically used for properties, such as `checked`, `selected`, `selectedIndex`, `tagName`, `nodeName`, `nodeType`, `ownerDocument`, `defaultChecked`, and `defaultSelected`. – Terry Apr 12 '14 at 20:55
  • You're right terry, that should say attr. Changed the code. Thanks for the hint. – Chris Apr 12 '14 at 21:04
  • @Chris - so close to working - These links are tagged `http://events.example.com/ehome/89335?discountcode=affiliate#Program` and `http://events.example.com/ehome/89335?discountcode=affiliate#Events` but the ones I need: `https://events.example.com/ereg/newreg.php?eventid=76355&categoryid=675329` and `http://events.example.com/89335` are not – joshttbnew Apr 12 '14 at 21:23
  • Hmm, did you changed the 'param' to 'discountcode' in the code? I updated the example again, is it working now? If not, did you checked that the not-processed links are included in the jQuery selector (to test, you can just execute the $('...') selector inside of the F12 (developer console) of your browser). – Chris Apr 12 '14 at 21:51
  • Hey @Chris, I had changed param to discount code before posting back. I changed the order jquery loaded in and the selector, and it works now. However, if there is no discount code it appends discountcode=null to the url, and the system tries to use "null" as a code. should I do: `if (persistedQueryParam != null) { $('a[href]').each(function () { var elem = $(this); var href = elem.attr('href'); elem.attr('href', href + (href.indexOf('?') != -1 ? '&' : '?') + 'discountcode=' + persistedQueryParam); }); }` – joshttbnew Apr 13 '14 at 22:13
  • That broke it; probably some punctuation or something I'm missing. – joshttbnew Apr 13 '14 at 22:20
  • Sorry for the late answer. Yes, you can just check if there's a query or not. But I would check the length, as it could be 'discountcode=' so you could get an empty string... Updated the answer with that. – Chris Apr 14 '14 at 10:23