-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
210 lines (181 loc) · 6.08 KB
/
script.liquid
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{% assign minimum_total_spent = options.minimum_total_spent__number_required %}
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% assign days_of_order_history = options.days_of_order_history_to_consider__number_required %}
{% assign monitor_customer_tag = options.only_monitor_customers_having_this_tag %}
{% if monitor_customer_tag == customer_tag_to_apply %}
{% error "The two customer tag values must be different. Please change either 'Customer tag to apply' or 'Only monitor customers having this tag'." %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- get IDs of all customers who meet the minimum spend criteria and have not yet been tagged
{% endcomment %}
{%- capture qualifying_search_query -%}
orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = true AND customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}'
{%- endcapture -%}
{% comment %}
-- if monitor tag configured, then also filter by that tag
{% endcomment %}
{% if monitor_customer_tag != blank %}
{% capture qualifying_search_query -%}
customer_tags CONTAINS '{{ monitor_customer_tag }}' AND {{ qualifying_search_query }}
{%- endcapture %}
{% endif %}
{% log qualifying_search_query: qualifying_search_query %}
{% assign cursor = nil %}
{% assign customer_segment_member_ids = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: {{ qualifying_search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/CustomerSegmentMember/1234567890"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer_segment_member_ids
= result.data.customerSegmentMembers.edges
| map: "node"
| map: "id"
| concat: customer_segment_member_ids
%}
{% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% unless event.preview %}
{% log count_of_customers_needing_tag_added: customer_segment_member_ids.size %}
{% endunless %}
{% for customer_segment_member_id in customer_segment_member_ids %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}
tags: {{ customer_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% comment %}
-- get IDs of all customers with tag who no longer meet the minimum spend criteria
{% endcomment %}
{%- capture disqualifying_search_query -%}
customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = false
{%- endcapture -%}
{% comment %}
-- if monitor tag configured, then also find customers without the monitor tag who have the minimum spend qualifying tag
{% endcomment %}
{% if monitor_customer_tag != blank %}
{% capture disqualifying_search_query -%}
customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND (customer_tags NOT CONTAINS '{{ monitor_customer_tag }}' OR orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = false)
{%- endcapture %}
{% endif %}
{% log disqualifying_search_query: disqualifying_search_query %}
{% assign cursor = nil %}
{% assign customer_segment_member_ids = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: {{ disqualifying_search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/CustomerSegmentMember/2345678901"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer_segment_member_ids
= result.data.customerSegmentMembers.edges
| map: "node"
| map: "id"
| concat: customer_segment_member_ids
%}
{% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% unless event.preview %}
{% log count_of_customers_needing_tag_removal: customer_segment_member_ids.size %}
{% endunless %}
{% for customer_segment_member_id in customer_segment_member_ids %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}
tags: {{ customer_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}