-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathauto-tag-customers-by-email-domain.json
24 lines (24 loc) · 5.59 KB
/
auto-tag-customers-by-email-domain.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 when customers are created, and it applies a tag if they have one of the configured email domains.\n\nUse the \"Run task\" button to scan all customers already registered.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers based on email domain",
"online_store_javascript": null,
"options": {
"customer_email_domains__required_array": [
"example.com"
],
"customer_tag_to_apply__required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign customer_email_domains = options.customer_email_domains__required_array %}\n{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}\n\n{% for email_domain in customer_email_domains %}\n {% if email_domain contains \"@\" %}\n {% error \"Do not include '@' symbols in email domains. Thanks!\" %}\n {% endif %}\n{% endfor %}\n\n{% assign customer_ids_to_tag = array %}\n\n{% if event.topic contains \"shopify/customers/\" %}\n {% if event.preview %}\n {% assign customer = hash %}\n {% assign customer[\"admin_graphql_api_id\"] = \"gid://shopify/Customer/1234567890\" %}\n {% assign customer[\"email\"] = \"test@\" | append: customer_email_domains.first %}\n {% endif %}\n\n {% assign customer_email_domain = customer.email | split: \"@\" | last | downcase %}\n {% assign customer_tags = customer.tags | split: \", \" %}\n\n {% if customer_email_domains contains customer_email_domain %}\n {% unless customer_tags contains customer_tag_to_apply %}\n {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.admin_graphql_api_id %}\n {% endunless %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign domain_query_parts = array %}\n\n {% for email_domain in customer_email_domains %}\n {% assign domain_query_parts[domain_query_parts.size]\n = \"customer_email_domain = '\"\n | append: email_domain\n | append: \"'\"\n %}\n {% endfor %}\n\n {% capture search_query -%}\n customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND ({{ domain_query_parts | join: \" OR \" }})\n {%- endcapture %}\n\n {% log search_query: search_query %}\n\n {% assign cursor = nil %}\n\n {% comment %}\n -- customers resource no longer supports tag filters (JUL 2024), so need to use the customerSegmentMembers resource instead\n {% endcomment %}\n\n {% for n in (0..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_segment_member_ids\n = result.data.customerSegmentMembers.edges\n | map: \"node\"\n | map: \"id\"\n %}\n\n {% for customer_segment_member_id in customer_segment_member_ids %}\n {% comment %}\n -- query the customer record in order to verify the domain and absence of the tag, in case the search query filter has \"seepage\"\n {% endcomment %}\n\n {% capture query %}\n query {\n customer(id: {{ customer_segment_member_id | remove: \"SegmentMember\" | json }}) {\n id\n email\n tags\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"email\": {{ \"test@\" | append: customer_email_domains.first | json }}\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer = result.data.customer %}\n {% assign customer_email_domain = customer.email | split: \"@\" | last | downcase %}\n\n {% if customer_email_domains contains customer_email_domain %}\n {% unless customer.tags contains customer_tag_to_apply %}\n {% assign customer_ids_to_tag = customer_ids_to_tag | push: customer.id %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.customers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% for customer_id in customer_ids_to_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer_id | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endfor %}\n",
"subscriptions": [
"shopify/customers/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/customers/create\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Customers"
]
}