0

TL;DR: I am forced to use a javascript solution to store a URL parameter as a cookie, but when the function runs, it only stores the first 5 characters of the value I need.

I am working on implementing affiliate sales tracking across domains. As stated above, I need to store a value from a URL in a cookie so that I can pull it into a separate (functioning) script later. On my primary domain, I was able to do this with a simple .php script, but the third-party platform we use for our sales doesn't allow me to run .php scripts, so I found a javascript function that seemed to be working prior to today. That said, prior to today I was using test parameters that were only numerical (1234567890, etc.).

Here is an example of the kind of URL and parameter being used:

https://subdomain.platform-domain.com/subscribe/Product_ID?irclickid=QW6QnmxpdxySWmnwUx0Mo6bwUkEx5HXJxUUm0c0

This is the function I've been using successfully up until now:

    function getParameterByName(name) {
      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
          results = regex.exec(location.search);
      return results === null
        ? ''
      : decodeURIComponent(results[1].replace(/\+/g, ' '));
    }
    var results = getParameterByName('irclickid');
    if (results != null || results != '') {
      Cookies.set('irclickid', results, { expires: 30 });
    }

For some reason, the function now only stores the first 5 characters of the value, or "QW6Qn" in this case. Any help or direction on how to make this work correctly is appreciated.

Resolution:

I found a function that was more apt for what I needed here on stackoverflow: How to get parameter name?, and replaced the first part of my javascript with the following, and it is now working as expected!

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

The last section remained the same:

    var results = getParameterByName('irclickid');
    if (results != null || results != '') {
      Cookies.set('irclickid', results, { expires: 30 });
    }

Thank you to those that offered help and insight.

1 Answers1

0
const url = new URL('https://subdomain.platform-domain.com/subscribe/Product_ID?irclickid=QW6QnmxpdxySWmnwUx0Mo6bwUkEx5HXJxUUm0c0')

document.cookie = `irclickid=${url.searchParams.get('irclickid')}; expires=...`

You can see on https://caniuse.com/url if all required browsers support URL.

Iyashi
  • 542
  • 4
  • 19
  • Thanks for responding. I don't know if that will work because you are specifying the domain in the script. I have to pull that dynamically, right? – notstoopid Oct 27 '20 at 18:01
  • You can use `document.URL` to get the current URL. (but I dunno if all browsers have it there.) – Iyashi Oct 27 '20 at 18:02