HubL & Query String

SOLVE
Highlighted
Occasional Contributor

I'm trying to set up a page so that it will personalize some content based on the query string. For example, if the query string is ?company=xyz, I would want the H1 to show up as "Welcome XYZ"

 

Here are the steps I took

1) Add custom HubL to a template: <h1>Welcome {{ request.query.company|default('Acme Corp')}}</h1>

2) Add the query string of ?company=xyz to the end of the url and test

 

The result is always the default of "Acme Corp" instead of what is in the query string. Any idea what I am doing wrong?

1 Accepted solution

Accepted Solutions
Esteemed Advisor

no problem. I understand, it's not exactly common practice javascript, at least not to me. Here a stack exchange post on the subject:

 

https://stackoverflow.com/questions/11526102/jquery-append-querystring-to-all-links

- Jonathan Sumner
Reply
0 Upvotes
6 Replies 6
Esteemed Advisor

@dtysick,

 

Try setting your query to a variable and outputting that variable onto your page first:

{% set query = request.query_dict.company %}

<p class="random_paragraph">
    {{ query }}
</p>

If it outputs, then try it without the filter:

<h1> Welcome {{ query }} </h1>

Then apply the filter again:

<h1> Welcome {{ query|default('Acme Corp') }} </h1>

Fact is there aren't to many things that can go wrong with something simple like this so strip it down to its most basic. It could possibly have something to do with the missing space in your h1.

- Jonathan Sumner
Occasional Contributor

Thanks! Made a small change and got it to work.

 

Is there an easy to way pass the query string to other pages as well? For example, they arrive at acme.com/product?company=test and they then click on pricing in the nav. I'd like the personalization to continue to that page so I would need the url to be acme.com/pricing?company=test.

Reply
0 Upvotes
Esteemed Advisor

@dtysick,

 

check out these http variables.

 

You should be able to add a variable behind your urls so that the urls always have the current query appended. This might be more difficult for advanced menus. You could also use javascript to capture the query and append it to all links on the page. 

- Jonathan Sumner
Occasional Contributor

Thanks for such a quick reply @Jsum. I'm not great with Javascript but I'll look around online and see if I can find something to modify.

Reply
0 Upvotes
Esteemed Advisor

no problem. I understand, it's not exactly common practice javascript, at least not to me. Here a stack exchange post on the subject:

 

https://stackoverflow.com/questions/11526102/jquery-append-querystring-to-all-links

- Jonathan Sumner
Reply
0 Upvotes
Occasional Contributor

Thanks! I actually found some jQuery that does the job pretty well:

 

<script>$("a").click(function(e) {
e.preventDefault();

var params = window.location.search,
dest = $(this).attr('href') + params;

// in my experience, a short timeout has helped overcome browser bugs
window.setTimeout(function() {
window.location.href = dest;
}, 100);
});
</script>