Operator
Liquid は、条件文で使用できる多くの 演算子 をサポートしています。このページでは、Liquid がサポートする演算子と、メッセージでの使用方法のユースケースを紹介します。
以下の表は、サポートされている演算子の一覧です。Liquid ではかっこは無効な文字であり、タグが正しく動作しなくなることに注意してください。
| 構文 | 演算子の説明 |
|---|---|
| == | 等しい |
| != | 等しくない |
| > | より大きい |
| < | より小さい |
| >= | 以上 |
| <= | 以下 |
| or | 条件 A または条件 B |
| and | 条件 A かつ条件 B |
| contains | 文字列または文字列配列に特定の文字列が含まれているかを確認する |
演算子は条件文(if、elsif、unless)で使用できますが、assign 文、for ループ、case/when 文、配列アクセスの角かっこでは使用できません。詳細については、演算子とフィルターの使用場所を参照してください。
かっこを使わずに条件をグループ化する
Liquid は式のグループ化にかっこをサポートしていません。(a and b) or c のような複雑なブール論理を評価するには、ネストされた if 文または中間変数を使用します。
たとえば、値が複合条件を満たすかどうかを確認するには、中間変数を割り当てます。
1
2
3
4
5
6
7
8
9
10
{% assign qualifies = false %}
{% if points > 100 %}
{% assign qualifies = true %}
{% elsif points == 100 and member_level == 'gold' %}
{% assign qualifies = true %}
{% endif %}
{% if qualifies %}
You qualify for a reward!
{% endif %}
チュートリアル
マーケティングキャンペーンでこれらの演算子を使用する方法を、いくつかのチュートリアルで学びましょう。
整数カスタム属性でメッセージを選択する
購入したことがあるユーザーとないユーザーに、パーソナライズされたプロモーション割引付きのプッシュ通知を送信しましょう。このプッシュ通知では、total_spend という整数カスタム属性を使用して、ユーザーの合計支出額を確認します。
- 大なり(
>)演算子を使用して条件文を記述し、ユーザーの合計支出額が0より大きいかどうか(つまり購入したことがあるかどうか)を確認します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% if {{custom_attribute.${total_spend}}} >0 %}
Surprise! We added a 15% discount code to your account that automatically applies to your next order.
{% else %}タグを追加して、合計支出額が0であるか、存在しないユーザーを捕捉します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% else %}
Need a sign to update your wardrobe? We added a 15% discount code to your account that will automatically apply to your first order.
{% endif %}タグで条件ロジックを閉じます。
1
{% endif %}

完全な Liquid コード
1
2
3
4
5
{% if {{custom_attribute.${total_spend}}} >0 %}
Surprise! We added a 15% discount code to your account that automatically applies to your next order.
{% else %}
Need a sign to update your wardrobe? We added a 15% discount code to your account that will automatically apply to your first order.
{% endif %}
これで、ユーザーの「Total Spend」カスタム属性が 0 より大きい場合、次のメッセージが届きます。
1
Surprise! We added a 15% discount code to your account that automatically applies to your next order.
ユーザーの「Total Spend」カスタム属性が存在しないか、0 に等しい場合、次のメッセージが届きます。
1
Need a sign to update your wardrobe? We added a 15% discount code to your account that will automatically apply to your first order.
文字列カスタム属性でメッセージを選択する
ユーザーにプッシュ通知を送信し、各ユーザーが最近プレイしたゲームに基づいてメッセージをパーソナライズしましょう。これには、recent_game という文字列カスタム属性を使用して、ユーザーが最後にプレイしたゲームを確認します。
- 等号(
==)演算子を使用して条件文を記述し、ユーザーの最近のゲームが Awkward Dinner Party かどうかを確認します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% if {{custom_attribute.${recent_game}}} == 'Awkward Dinner Party' %}
You are formally invited to our next dinner party. Log on next week for another round of delectable dishes and curious conversations.
elsifタグと等号(==)演算子を使用して、ユーザーの最近のゲームが Proxy War 3: War of Thirst かどうかを確認します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% elsif {{custom_attribute.${recent_game}}} == 'Proxy War 3: War of Thirst' %}
Your fleet awaits your next orders. Log on when you're ready to rejoin the war for hydration.
elsifタグと「等しくない」(!=)および「かつ」(and)演算子を使用して、ユーザーに最近のゲームがあるか(つまり値が空白でないか)、かつそのゲームが Awkward Dinner Party でも Proxy War 3: War of Thirst でもないかを確認します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% elsif {{custom_attribute.${recent_game}}} != blank and {{custom_attribute.${recent_game}}} != 'Awkward Dinner Party' and {{custom_attribute.${recent_game}}} != 'Proxy War 3: War of Thirst' %}
Limited Time Deal! Get 15% off our best-selling classics!
{% else %}タグを追加して、最近のゲームがないユーザーを捕捉します。次に、そのユーザーに送信するメッセージを作成します。
1
2
{% else %}
Hey! I've got a deal for you. Buy 2 of our newest releases and get 10% off!
{% endif %}タグで条件ロジックを閉じます。
1
{% endif %}
完全な Liquid コード
1
2
3
4
5
6
7
8
9
{% if {{custom_attribute.${recent_game}}} == 'Awkward Dinner Party' %}
You are formally invited to our next dinner party. Log on next week for another round of delectable dishes and curious conversations.
{% elsif {{custom_attribute.${recent_game}}} == 'Proxy War 3: War of Thirst' %}
Your fleet awaits your next orders. Log on when you're ready to rejoin the war for hydration.
{% elsif {{custom_attribute.${recent_game}}} != blank and {{custom_attribute.${recent_game}}} != 'Awkward Dinner Party' and {{custom_attribute.${recent_game}}} != 'Proxy War 3: War of Thirst' %}
Limited Time Deal! Get 15% off our best-selling classics!
{% else %}
Hey! I've got a deal for you. Buy 2 of our newest releases and get 10% off!
{% endif %}

これで、ユーザーが最後に Awkward Dinner Party をプレイした場合、次のメッセージが届きます。
1
You are formally invited to our next dinner party. Log on next week for another round of delectable dishes and curious conversations.
ユーザーの最近のゲームが Proxy War 3: War of Thirst の場合、次のメッセージが届きます。
1
Your fleet awaits your next orders. Log on when you're ready to rejoin the war for hydration.
ユーザーが最近プレイしたゲームが Awkward Dinner Party でも Proxy War 3: War of Thirst でもない場合、次のメッセージが届きます。
1
Limited Time Deal! Get 15% off our best-selling classics!
ユーザーがゲームをプレイしたことがないか、そのカスタム属性がプロファイルに存在しない場合、次のメッセージが届きます。
1
Hey! I've got a deal for you. Buy 2 of our newest releases and get 10% off!
ロケーションに基づいてメッセージを中止する
ほぼあらゆる条件に基づいてメッセージを中止できます。ユーザーが指定されたエリアに拠点を置いていない場合にメッセージを中止しましょう。プロモーション、ショー、配送の対象外となる可能性があるためです。
- 等号(
==)演算子を使用して条件文を記述し、ユーザーのタイムゾーンがAmerica/Los_Angelesかどうかを確認し、そのユーザーに送信するメッセージを作成します。
1
2
{% if {{${time_zone}}} == 'America/Los_Angeles' %}
Stream now!
America/Los_Angelesタイムゾーン外のユーザーにメッセージを送信しないようにするため、{% else %}タグと{% endif %}タグで{% abort_message () %}タグを囲みます。
1
2
3
{% else %}
{% abort_message () %}
{% endif %}
完全な Liquid コード
1
2
3
4
5
{% if {{${time_zone}}} =='America/Los_Angeles' %}
Stream now!
{% else %}
{% abort_message () %}
{% endif %}

コネクテッドコンテンツに基づいてメッセージを中止することもできます。
トラブルシューティング
プレビューでプロパティの型が誤って変換される場合がある
ダッシュボードでメッセージをプレビューする際、ほとんどの変数(カスタム属性など)は正しい型に変換されます。ただし、一部の変数にはプレビューが参照できる定義済みの型がありません。
api_trigger_propertiescanvas_entry_propertiescontext
これらのプロパティについては、プレビューは値から型を推測しようとします。つまり、文字列として意図した値が数値として誤って解釈される可能性があります。たとえば、プロパティの値が文字列 "3" の場合、プレビューはそれを整数 3 に変換することがあり、contains や split などの文字列操作で予期しない動作が発生する可能性があります。
これらのプロパティタイプを使用する際にプレビューで予期しない結果が表示された場合、プレビューの型推測が送信時の動作と一致しない可能性があることに留意してください。送信時には、トリガーイベントまたは API 呼び出しからの実際のデータ型が保持されます。
プレビューで特定の型を強制するには、値を明示的にキャストできます。
1
2
3
4
5
{% comment %} Force a value to be treated as a number {% endcomment %}
{% assign orders = {{canvas_entry_properties.${number_of_orders}}} | plus: 0 %}
{% comment %} Force a value to be treated as a string {% endcomment %}
{% assign code = {{api_trigger_properties.${promo_code}}} | append: "" %}