How to pass a parameter and its value from url to liquid

How to pass a parameter and its value from url to liquid

Arjun6
Visitor
1 0 4

Is there any way  i can get the parameter and its value from url. For example,

www.test.com/?parameter=value

I should get parameter and value to a variable in liquid.

Replies 8 (8)

Jason
Shopify Partner
11206 226 2311

What's the use case? In most cases expect the answer to be no but there's some hacky approaches that can work depending on where you want this to happen.

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★

Nelson_Fisher
Visitor
2 0 1

Here is my case, hopefully this example applies. 

  1. Customer is sent to the site via affiliate in the cj network. 
  2. Within the URL, there is a "cjevent=ABC123" parameter. 
  3. I want to store that as a cart attribute or in a cookie, somewhere that is readable later in the customer's journey. 
  4. Customer checks out. 
  5. I pass the given parameter in the response to the cj tracking pixel, listed in the additional scripts in checkout. 

Any help here would be appreciated! Cheers!

INTERSTELLAR
Visitor
2 0 0

Hey Nelson, did you ever get any insight into this from folks? Or perhaps figure it out on your own? We're trying to do the same CJEVENT update, but can't get it to work. Any insight would be greatly appreciated!

Nelson_Fisher
Visitor
2 0 1

Hi Interstellar,

I solved it by installing GTM and using Javascript to handle it. 

If you have Shopify Plus:Use this to install GTM: https://help.shopify.com/en/manual/reports-and-analytics/google-analytics/google-tag-manager (You'll have to ask your account manager to give you access to the checkout template.)

If you do not have plus, you'll have to figure out a different way to install GTM. I'm sure there are apps that'll help you do it.  

 

Then just create the following tags, variables, and triggers. 

cjevent - Tag

<script>
 
 var cookieName = "cjevent"; // Name of your cookie
 var cookieValue = {{URL- cjevent}}; // Value of your cookie
 var expirationTime = 604800; // in seconds
 expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
 var date = new Date(); 
 var dateTimeNow = date.getTime(); 

 date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + expiration time)
 var date = date.toUTCString(); // Converts milliseconds to UTC time string
 document.cookie = cookieName+"="+cookieValue+"; expires="+date+"; path=/; domain=." + location.hostname.replace(/^www\./i, ""); // Sets cookie for all subdomains

</script>

Tag firing priority of 99

 

cjevent trigger 

Page URL contains cjevent

 

cjevent purchase Tag (replace *** with your CID & Type)

<script type="text/javascript">

sendCJTracking();

function sendCJTracking() {
  var cjEvent = {{1st Party Cookie}};
  var purchase = Shopify.checkout;
  var cart = purchase.line_items;
  var queryString = "";
  var itemsDiscounts = 0;
  var j = 0;

  var itemsDiscounts;
  for (i = 0; i < cart.length; i++) {
    j = j + 1;
    if (j == 1) {
      queryString = "CID=*******&OID=" + purchase.order_id.toString();
      if (i == 0) {
        if (purchase.discount.code) {
            queryString += "&COUPON=" + purchase.discount.code.replace(";", ",");
        }
      }
    }

    if (!isNaN(parseFloat(purchase.discount.amount))) {
      itemsDiscounts = purchase.discount.amount.toString();
    }

    queryString += "&ITEM" + (i + 1).toString() + "=" + cart[i].sku.toString()
    + "&AMT" + (i + 1).toString() + "=" + cart[i].line_price.toString()
    + "&QTY" + (i + 1).toString() + "=" + cart[i].quantity.toString();

    if (j == 50) {
      queryString += "&CURRENCY=GBP&METHOD=IMG";
      if (i == cart.length) {
          if (itemsDiscounts) {
            queryString += "&DISCOUNT=" + itemsDiscounts;
        }
      }
      j = 0;
      if (cjEvent) {
        queryString += "&CJEVENT=" + cjEvent + "&TYPE=******";
      }
      addCJTracking(queryString);
    }
  }
  if (j != 50) {
    queryString += "&CURRENCY=GBP&METHOD=IMG";
    if (itemsDiscounts) {
        queryString += "&DISCOUNT=" + itemsDiscounts;
    }
    if (cjEvent) {
      queryString += "&CJEVENT=" + cjEvent + "&TYPE=******";
    }
    addCJTracking(queryString);
  }
}

function addCJTracking(queryString) {
  var url_src = '//www.emjcd.com/u?' + queryString;
  var dcIMG = document.createElement('img');
  dcIMG.setAttribute('src', url_src);
  dcIMG.setAttribute('height','1');
  dcIMG.setAttribute('width','1');
  dcIMG.setAttribute('border','0');
  dcIMG.setAttribute('style','display:none');
  document.body.appendChild(dcIMG); 
}

</script>

Tag firing priority (silly I know) :1000000000000000000

 

cjevent purchase trigger

Page URL contains thank_you

 

1st Party Cookie Variable

Variable type=1st party cookie

Cookie Name: cjevent

 

URL- cjevent

Variable Type: URL

Query Key: cjevent

 

 

 

 

Prabhat
Shopify Partner
84 8 29

Hey here is a way we can achieve the same 

https://freakdesign.com.au/blogs/news/get-the-url-querystring-values-with-liquid-in-shopify

Solution Expert
- Did I solved your issue? Like & Mark As Solution to help the community.

Checkout Rules and Content - Checkout Pro
billy-levitate
Shopify Partner
1 0 1

Is there any update on this? This is a basic back end function and to have no access to it in Liquid really limits customization options when passing variables.

TomTranzistor
Shopify Partner
62 4 15

try this 🙂

 

{%- capture contentForQuerystring -%}{{ content_for_header }}{%- endcapture -%}
{%- assign plan_name = contentForQuerystring | split:'"pageurl":"' | last | split:'"' | first |
   replace:'\/','/' |
   replace:'%20',' ' |
   replace:'\u0026','&' |
   split: "?" | last
-%}

 

 

zina2
Shopify Partner
1 0 0

how to get the content_for_header?