Tag online orders by ?ls= Locksmith secret link used
Tag orders with the Locksmith secret link used when placing an order. Optionally, tag the customer as well.
Try this taskWhen it runs
Occurs whenever an order is created
What you choose
- Query parameter name
- Cart attribute name
- Tag customer with parameter value
- Only tag the customer for their first order
Documentation
Tag orders with the Locksmith secret link used when placing an order. Optionally, tag the customer as well.
Enable Mechanic in your theme
This task needs to run JavaScript in your online store. If Mechanic shows a theme setup prompt, click Enable Mechanic in your theme, make sure Mechanic's Online store JavaScript is turned on under App embeds, and click Save in the theme editor. Then return to Mechanic to confirm setup.
You only need to do this once for your current theme; the same setting serves all your enabled tasks that use storefront JavaScript. You can leave it enabled if you disable this task. If your store doesn't already have Mechanic loading JavaScript, this task's storefront functionality won't work until setup is complete. Learn about theme setup and existing tasks.
How this task works
Note: if a customer uses multiple secret links, the last one used is assigned to the order.
This task adds JavaScript to your storefront, which adds ?ls= parameter (Locksmith secret link) to the customer's cart (using the cart attribute name of your choice). (Locksmith secret link values are saved in a browser cookie for one year.) This task copies the cart attribute to the order's tags when the corresponding order is created. Optionally, select to have the value applied to the customer as a tag as well.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/orders/create
How task subscriptions work
Option types and flags
- Query parameter name (required)
- Cart attribute name (required)
- Tag customer with parameter value (boolean)
- Only tag the customer for their first order (boolean)
Task code
{% comment %}
An opinion about option order
{{ options.query_parameter_name__required }}
{{ options.cart_attribute_name__required }}
{{ options.tag_customer_with_parameter_value__boolean }}
{{ options.only_tag_the_customer_for_their_first_order__boolean }}
{% endcomment %}
{% if event.preview %}
{% assign order = hash %}
{% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% assign order["note_attributes"] = hash %}
{% assign order["note_attributes"][options.cart_attribute_name__required] = "ABC123" %}
{% assign order["customer"] = hash %}
{% assign order["customer"]["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
{% endif %}
{% assign value = order.note_attributes[options.cart_attribute_name__required] %}
{% if value == blank %}
{% log message: "This order did not have the specified cart attribute - nothing to do.", attributes: order.note_attributes %}
{% else %}
{% assign order_tags = order.tags | split: ", " %}
{% unless order_tags contains value %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.admin_graphql_api_id | json }}
tags: {{ value | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endif %}
{% if options.tag_customer_with_parameter_value__boolean %}
{% assign customer_qualifies = true %}
{% assign customer_tags = order.customer.tags | split: ", " %}
{% if customer_tags contains value %}
{% assign customer_qualifies = false %}
{% endif %}
{% if options.only_tag_the_customer_for_their_first_order__boolean %}
{% comment %}
We go the long way about this, because (a) in a test store,
Shopify is not incrementing customer.orders_count, and (b)
it does not seem like the order reliably exists for retrieval
the instant it is placed. This way, we can mark the customer
as qualified if there are no orders, or if there is an order
and this is it.
{% endcomment %}
{% capture query %}
query {
customer(id: {{ order.customer.admin_graphql_api_id | json }}) {
tags
orders(first: 1) {
pageInfo {
hasNextPage
}
edges {
node {
id
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"tags": [],
"orders": {
"pageInfo": {
"hasNextPage": false
},
"edges": [
{
"node": {
"id": {{ order.admin_graphql_api_id | json }}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% if result.data.customer.orders.pageInfo.hasNextPage %}
{% assign customer_qualifies = false %}
{% elsif result.data.customer.orders.edges != empty and result.data.customer.orders.edges.first.node.id != order.admin_graphql_api_id %}
{% assign customer_qualifies = false %}
{% endif %}
{% endif %}
{% if customer_qualifies and value != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.customer.admin_graphql_api_id | json }}
tags: {{ value | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
Mechanic Liquid reference
Online store JavaScript
This code runs after the storefront page loads. Enable Mechanic’s Online store JavaScript app embed in your published theme and save the theme to finish setup.
// https://gist.github.com/bronson/6707533
var cookie = function(name, value, ms) {
if(arguments.length < 2) {
// read cookie
var cookies = document.cookie.split(';')
for(var i=0; i < cookies.length; i++) {
var c = cookies[i].replace(/^\s+/, '')
if(c.indexOf(name+'=') == 0) {
return decodeURIComponent(c.substring(name.length+1).split('+').join(' '))
}
}
return null
}
// write cookie
var date = new Date()
date.setTime(date.getTime()+ms)
document.cookie = name+"=" + encodeURIComponent(value) + (ms ? ";expires="+date.toGMTString() : '') + ";path=/"
}
var parameterName = {{ options.query_parameter_name__required | json }};
var valueKeyval = window.location.search.split(/[?&]/).filter(function (keyval) {
return keyval.slice(0, parameterName.length) === parameterName;
})[0];
var valueFromCookie = cookie(parameterName);
if (valueFromCookie || valueKeyval) {
var value = decodeURIComponent(valueKeyval.split('=')[1]) || valueFromCookie;
if (!valueFromCookie) {
cookie(parameterName, value, 1000*60*60*24*365);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '/cart/update.json', true);
//Send the proper header information along with the request
xhr.setRequestHeader('Content-Type', 'application/json');
var attributes = {};
attributes[{{ options.cart_attribute_name__required | json }}] = value;
xhr.send(JSON.stringify({ attributes: attributes }));
}
Default option values
- Query parameter name
ls- Cart attribute name
Locksmith Secret Link- Tag customer with parameter value
false- Only tag the customer for their first order
false