Wix - append query string parameters

Our marketing team will be promoting wix landing pages which will contain all the fancy utm codes as query string parameters when someone lands on the wix page.

What I want to achieve is that for every link that goes out from the wix landing page, it should append whatever is in the query string so that it gets tracked into our application.

I was trying to do it in a very manual way by adding code to each and every click event of the button that exists in my wix landing page, however even that does not seem to be working :

export function button6_click(event) {
let query = wixLocation.query;
wixLocation.to(“https://mysite.com?” + query)
}

When I click the button it takes me to :
https://mysite.com?[object%20Object]

Hey there!
That is because the query property returns an object of all the queries you have in your url. In the API Reference you can read the below.

import wixLocation from 'wix-location';

// ...

let query = wixLocation.query; // {"species": "african-elephant"}

That means that if you have like 10 query parameters you will need to loop through them all and make the new query object. I would suggest you look at the url parameter instead like the below.

let url = wixLocation.url;
// Will consists of https://domain.com/animals/mammals/elephant?species=african-elephant&utm=656565&type=commercial&test=true"

So just create a function that will get all after ? because there is all your query parameters.

function getParametersAsString(url) {
 return url.substring(url.indexOf("?"), url.length)
}

Then you call that function when you are to redirect your users like this.

wixLocation.to("your new url?" + getParametersAsString(wixLocation.url));

Or create a function that will walk through all query parameters and then make a string out of them and you can attach that to all wixLocation.to()

function getAllQueryParameters(queryObject) {
 let allString = "";
 var queryItems = Object.keys(queryObject).map(function(key) {
 return [key, queryObject[key]];
    });
    queryItems.forEach((item) => {
        allString += item[0] + "=" + item[1] + "&";
    })
 return allString;
}

Use it like

wixLocation.to("your new url?" + getAllQueryParameters(wixLocation.query));

Hope some helps! Good luck! If you think my answer helped please mark it TOP COMMENT as it will help others to find this solution.

1 Like

Is anyone facing this issue? I can’t seem to make this work. Ended up making it work by doing this:

$w.onReady(() => {
//   let referral = wixLocation.query.referral;
//   if (referral)
//     $w('#button8').link = 
//       'https://google.com' + referral;
//       $w('#button99').link = 
//       https://google.com' + referral;
// 	  if (referral)
//     $w('#button60').link = 
//       https://google.com' + referral;

I did this for every single query para and for every button I have.

Unfortunately, this setup does not accept utm_campaign. Only single words.

Does anyone have a better solution?

Trying to append all query params to any link on my page. Sigh

You can try

$w('Button').link += referral;

It’ll most likely append the referral to each link on the page.