-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathauto-tag-customers-with-who-have-accounts.json
24 lines (24 loc) · 7.2 KB
/
auto-tag-customers-with-who-have-accounts.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
{
"docs": "This task runs automatically for individual customers, as they are created and updated. It will tag customers when they have an enabled account (i.e. have a registered email address and active password), and it will untag customers whose accounts are disabled.\n\nRun this task manually to scan all existing customers, tagging and untagging as appropriate.\n\nUse test mode to have this task return information about what actions it would normally take.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers with who have accounts",
"online_store_javascript": null,
"options": {
"customer_tag_to_apply__required": null,
"test_mode__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% if event.topic contains \"shopify/customers/\" %}\n {% if event.preview %}\n {% assign customer = hash %}\n {% assign customer[\"state\"] = \"enabled\" %}\n {% assign customer[\"tags\"] = \"\" %}\n {% assign customer[\"admin_graphql_api_id\"] = \"gid://shopify/Customer/1234567890\" %}\n {% endif %}\n\n {% assign customer_tags = customer.tags | split: \", \" %}\n\n {% if customer.state == \"enabled\" %}\n {% unless customer_tags contains customer_tag_to_apply %}\n {% if test_mode %}\n {% log \"This customer should be tagged. (Doing nothing, because test mode is enabled.\" %}\n\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endunless %}\n\n {% elsif customer_tags contains customer_tag_to_apply %}\n {% if test_mode %}\n {% log \"This customer should be untagged. (Doing nothing, because test mode is enabled.\" %}\n\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- get IDs of all customers who have accounts and have not yet been tagged\n {% endcomment %}\n\n {% assign customer_ids_to_tag = array %}\n {% assign cursor = nil %}\n\n {%- capture search_query -%}\n customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND customer_account_status = 'ENABLED'\n {%- endcapture -%}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customerSegmentMembers(\n first: 1000\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customerSegmentMembers\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/CustomerSegmentMember/1234567890\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer_ids_to_tag\n = result.data.customerSegmentMembers.edges\n | map: \"node\"\n | map: \"id\"\n | concat: customer_ids_to_tag\n %}\n\n {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- get IDs of all customers with tag who no longer have accounts\n {% endcomment %}\n\n {% assign customer_ids_to_untag = array %}\n {% assign cursor = nil %}\n\n {%- capture search_query -%}\n customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND customer_account_status != 'ENABLED'\n {%- endcapture -%}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customerSegmentMembers(\n first: 1000\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customerSegmentMembers\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/CustomerSegmentMember/2345678901\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer_ids_to_untag\n = result.data.customerSegmentMembers.edges\n | map: \"node\"\n | map: \"id\"\n | concat: customer_ids_to_untag\n %}\n\n {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% log\n count_customers_to_tag: customer_ids_to_tag.size,\n count_customers_to_untag: customer_ids_to_untag.size\n %}\n\n {% unless test_mode %}\n {% for customer_id in customer_ids_to_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer_id | remove: \"SegmentMember\" | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n\n {% for customer_id in customer_ids_to_untag %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer_id | remove: \"SegmentMember\" | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endunless %}\n{% endif %}\n",
"subscriptions": [
"shopify/customers/create",
"shopify/customers/update",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/customers/create\nshopify/customers/update\nmechanic/user/trigger",
"tags": [
"Account",
"Auto-Tag",
"Customers"
]
}