- "script": "{% assign customer_tags_and_days_since_last_order = options.customer_tags_and_days_since_last_order__keyval_number_required %}\n{% assign create_customer_segments_by_tag = options.create_customer_segments_by_tag__boolean %}\n{% assign now_s = \"now\" | date: \"%s\" %}\n\n{% assign tag_rules = array %}\n\n{% for keyval in customer_tags_and_days_since_last_order %}\n {% assign customer_tag = keyval[0] %}\n {% assign days_since_last_order = keyval[1] %}\n\n {% if customer_tag == blank or days_since_last_order == blank or days_since_last_order < 0 %}\n {% continue %}\n {% endif %}\n\n {% assign tag_rule = hash %}\n {% assign tag_rule[\"days\"] = days_since_last_order %}\n {% assign tag_rule[\"tag\"] = customer_tag %}\n {% assign tag_rule[\"threshold_s\"]\n = days_since_last_order\n | times: -86400\n | plus: now_s\n %}\n {% assign tag_rules = tag_rules | push: tag_rule %}\n{% endfor %}\n\n{% assign tag_rules = tag_rules | sort: \"days\" | reverse %}\n{% assign configured_tags = tag_rules | map: \"tag\" %}\n\n{% if tag_rules == blank %}\n {% error \"Please enter at least one customer tag and days since last order combo.\" %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- get IDs of all customers who have placed an order\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign customer_ids = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customerSegmentMembers(\n first: 1000\n after: {{ cursor | json }}\n query: \"number_of_orders > 0\"\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 {% assign customer_segment_members = result.data.customerSegmentMembers.edges | map: \"node\" %}\n\n {% comment %}\n -- remove the \"SegmentMember\" portion from IDs for easier use in querying each customer for additional data not available in the segment resource\n {% endcomment %}\n\n {% for customer_segment_member in customer_segment_members %}\n {% assign customer_ids[customer_ids.size] = customer_segment_member.id | remove: \"SegmentMember\" %}\n {% endfor %}\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 {% unless event.preview %}\n {% log\n count_of_customers_who_have_placed_an_order: customer_ids.size,\n tag_rules: tag_rules\n %}\n {% endunless %}\n\n {% if event.preview %}\n {% assign customer_ids[0] = \"gid://shopify/Customer/1234567890\" %}\n {% endif %}\n\n {% for customer_id in customer_ids %}\n {% comment %}\n -- get customer tags and last order date\n {% endcomment %}\n\n {% capture query %}\n query {\n customer(id: {{ customer_id | json }}) {\n id\n tags\n lastOrder {\n processedAt\n }\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 \"lastOrder\": {\n \"processedAt\": {{ tag_rules[0].threshold_s | json }}\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer = result.data.customer %}\n\n {% comment %}\n -- determine the tag that the customer should have based their last order date\n {% endcomment %}\n\n {% assign customer_last_order_processed_at_s = customer.lastOrder.processedAt | date: \"%s\" | times: 1 %}\n {% assign tag_should_have = nil %}\n\n {% for tag_rule in tag_rules %}\n {% if customer_last_order_processed_at_s <= tag_rule.threshold_s %}\n {% assign tag_should_have = tag_rule.tag %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless tag_should_have == blank or customer.tags contains tag_should_have %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tag_should_have | json }}\n ) {\n node {\n ... on Customer {\n id\n email\n lastOrder {\n name\n processedAt\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n\n {% comment %}\n -- remove any other configured tags from the customer if needed\n {% endcomment %}\n\n {% assign tags_to_remove = array %}\n\n {% for configured_tag in configured_tags %}\n {% unless configured_tag == tag_should_have %}\n {% if customer.tags contains configured_tag %}\n {% assign tags_to_remove = tags_to_remove | push: configured_tag %}\n {% endif %}\n {% endunless %}\n {% endfor %}\n\n {% if tags_to_remove != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.id | json }}\n tags: {{ tags_to_remove | json }}\n ) {\n node {\n ... on Customer {\n id\n email\n lastOrder {\n name\n processedAt\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- if task option enabled, then create customer segments for each configured tag (if they don't already exist)\n {% endcomment %}\n\n {% if create_customer_segments_by_tag %}\n {% assign cursor = nil %}\n {% assign segments = array %}\n\n {% for n in (1..10) %}\n {% capture query %}\n query {\n segments(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n name\n query\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign segments\n = result.data.segments.nodes\n | default: array\n | concat: segments\n %}\n\n {% if result.data.segments.pageInfo.hasNextPage %}\n {% assign cursor = result.data.segments.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% for tag_rule in tag_rules %}\n {%- capture segment_rule -%}\n customer_tags CONTAINS '{{ tag_rule.tag }}'\n {%- endcapture -%}\n\n {% assign matched_segment\n = segments\n | where: \"name\", tag_rule.tag\n | where: \"query\", segment_rule\n %}\n\n {% if matched_segment != blank %}\n {% log\n message: \"This customer segment already exists.\",\n matched_segment: matched_segment\n %}\n {% continue %}\n {% endif %}\n\n {% action \"shopify\" %}\n mutation {\n segmentCreate(\n name: {{ tag_rule.tag | json }}\n query: {{ segment_rule | json }}\n ) {\n segment {\n id\n name\n query\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n{% endif %}\n",
0 commit comments