Skip to content

Instantly share code, notes, and snippets.

@gbrits
Created April 14, 2020 04:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbrits/6dc2d72d2b63d0b278c7c1b16e5492f1 to your computer and use it in GitHub Desktop.
Save gbrits/6dc2d72d2b63d0b278c7c1b16e5492f1 to your computer and use it in GitHub Desktop.
Shopify $_GET variables via Liquid
<!-- Add this at the top of your page template -->
{%- capture contentForQuerystring -%}{{ content_for_header }}{%- endcapture -%}
{%- assign pageUrl = contentForQuerystring | split:'"pageurl":"' | last | split:'"' | first | split:'.myshopify.com' | last |
replace:'\/','/' |
replace:'%20',' ' |
replace:'\u0026','&'
-%}
{%- unless pageUrl contains "?" -%}{% break %}{%- endunless -%}
{%- assign pageQuerystring = pageUrl | split:'?' | last -%}
{%- assign tag = pageQuerystring | split:'=' | last -%}
{%- assign query_tags = tag | split: "," -%}
<!-- Try adding ?tag=blah,blah2,blah3" at the end of your page's URL. -->
<!-- // Example: -->
{{ query_tags | json }}
@Christopher-Hayes
Copy link

⚠️ Beware - contentForQuerystring gets cached by Shopify. If you were to revisit the same page with a slightly different URL param value, contentForQuerystring will give you the wrong (cached) param values.

@gbrits
Copy link
Author

gbrits commented May 13, 2022

Hi @Christopher-Hayes, you're right. I have successfully used the above for minor filtering, but it is an imperfect solution. It is also (now) an outdated solution, with the advent of 2.0 Themes in Shopify.

@mav33rick
Copy link

@gbrits I was hoping you might help me. What is the current solution to grab the UTM parameters and pass them into liquid variables to use as hidden fields in form submits? Thanks!

@Christopher-Hayes
Copy link

Christopher-Hayes commented Dec 10, 2022

@gbrits I was hoping you might help me. What is the current solution to grab the UTM parameters and pass them into liquid variables to use as hidden fields in form submits? Thanks!

You can try looking at the request object, but I think that's just the URL (without params). Pre-2.0 Shopify didn't really support it, and Post-2.0 that hasn't changed. Using contentForQuerystring is the only method I'm aware of. So, your only real option is to move the logic into the browser with some js running after the page loads.

There's also Storefront filtering on collection pages and search pages. Which might help you. It seems limited in what parms you can use, but might be enough for your use-case.

@gbrits
Copy link
Author

gbrits commented Apr 30, 2024

For now it's probably just best to stick to good ol' JavaScript:

const params = new URLSearchParams(window.location.search);
const value = params.get('email'); // ?email=test for example...
console.log(value);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment