-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-tag-customers-that-accept-marketing.json
25 lines (25 loc) · 6.84 KB
/
auto-tag-customers-that-accept-marketing.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"docs": "Use this task to auto-tag customers whose marketing preferences have been updated, adding a tag for those that do accept marketing and removing it from customers who do not. Optionally, choose whether to limit tags to customers who have single or confirmed opt-in levels. Run this task manually to scan all customers at once.\n\n[Learn more from Shopify](https://help.shopify.com/en/manual/promoting-marketing/create-marketing/customer-emails#enable-double-opt-in-for-subscribers) about confirmed opt-in (aka double opt-in), and how to configure this for your store.\n\nNote: Running this task manually will sync that tag for **ALL** customers that accept marketing (and if checked, with required opt-in level), regardless of when their marketing preferences were last updated. If you have many customers, running this task manually could take a long time!",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers that accept marketing",
"online_store_javascript": null,
"options": {
"customer_tag_to_add__required": null,
"only_tag_confirmed_opt_ins__boolean": null,
"tag_single_and_confirmed_opt_ins__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign customer_tag_to_add = options.customer_tag_to_add__required %}\n{% assign only_tag_confirmed_opt_ins = options.only_tag_confirmed_opt_ins__boolean %}\n{% assign tag_both_single_and_confirmed_opt_ins = options.tag_both_single_and_confirmed_opt_ins__boolean %}\n\n{% if only_tag_confirmed_opt_ins and tag_both_single_and_confirmed_opt_ins %}\n {% error \"Choose either 'Only tag confirmed opt-ins' or 'Tag both single and confirmed opt-ins', or neither, but not both\" %}\n{% endif %}\n\n{% assign customers = array %}\n\n{% if event.topic contains \"shopify/customers/\" %}\n {% capture query %}\n query {\n customer(id: {{ customer.admin_graphql_api_id | json }}) {\n id\n tags\n email\n emailMarketingConsent {\n consentUpdatedAt\n marketingState\n marketingOptInLevel\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n {% assign customers[0] = result.data.customer %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n customers {\n edges {\n node {\n __typename\n id\n tags\n email\n emailMarketingConsent {\n consentUpdatedAt\n marketingState\n marketingOptInLevel\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign customers = bulkOperation.objects | where: \"__typename\", \"Customer\" %}\n{% endif %}\n\n{% if event.preview %}\n {% capture customers_json %}\n [\n {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": [],\n \"emailMarketingConsent\": {\n \"marketingState\": \"SUBSCRIBED\",\n \"marketingOptInLevel\": \"SINGLE_OPT_IN\"\n }\n },\n {\n \"id\": \"gid://shopify/Customer/2345678901\",\n \"tags\": [{{ customer_tag_to_add | json }}],\n \"emailMarketingConsent\": {\n \"marketingState\": \"NOT_SUBSCRIBED\",\n \"marketingOptInLevel\": null\n }\n },\n {\n \"id\": \"gid://shopify/Customer/3456789012\",\n \"tags\": [{{ customer_tag_to_add | json }}],\n \"emailMarketingConsent\": {\n \"marketingState\": \"SUBSCRIBED\",\n \"marketingOptInLevel\": \"SINGLE_OPT_IN\"\n }\n },\n {\n \"id\": \"gid://shopify/Customer/4567890123\",\n \"tags\": [],\n \"emailMarketingConsent\": {\n \"marketingState\": \"SUBSCRIBED\",\n \"marketingOptInLevel\": \"CONFIRMED_OPT_IN\"\n }\n }\n ]\n {% endcapture %}\n\n {% assign customers = customers_json | parse_json %}\n{% endif %}\n\n{% for customer in customers %}\n {% assign qualifies_for_tag = nil %}\n\n {% if only_tag_confirmed_opt_ins %}\n {% if customer.emailMarketingConsent.marketingOptInLevel == \"CONFIRMED_OPT_IN\" %}\n {% if customer.emailMarketingConsent.marketingState == \"SUBSCRIBED\" %}\n {% assign qualifies_for_tag = true %}\n {% endif %}\n {% endif %}\n\n {% elsif tag_both_single_and_confirmed_opt_ins %}\n {% if customer.emailMarketingConsent.marketingOptInLevel == \"CONFIRMED_OPT_IN\" or customer.emailMarketingConsent.marketingOptInLevel == \"SINGLE_OPT_IN\" %}\n {% if customer.emailMarketingConsent.marketingState == \"SUBSCRIBED\" %}\n {% assign qualifies_for_tag = true %}\n {% endif %}\n {% endif %}\n\n {% elsif customer.emailMarketingConsent.marketingState == \"SUBSCRIBED\" %}\n {% assign qualifies_for_tag = true %}\n {% endif %}\n\n {% if qualifies_for_tag %}\n {% unless customer.tags contains customer_tag_to_add %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ customer_tag_to_add | json }}\n ) {\n node {\n ... on Customer {\n email\n tags_after_add: tags\n emailMarketingConsent {\n consentUpdatedAt\n marketingState\n marketingOptInLevel\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n\n {% elsif customer.tags contains customer_tag_to_add %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.id | json }}\n tags: {{ customer_tag_to_add | json }}\n ) {\n node {\n ... on Customer {\n email\n tags_after_remove: tags\n emailMarketingConsent {\n consentUpdatedAt\n marketingState\n marketingOptInLevel\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}\n",
"subscriptions": [
"shopify/customers/update",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/customers/update\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Customers",
"Marketing"
]
}