
What is Nesting?
When a complex segment like the above example is to be created, we will need to group some of the segment blocks. Logically, if the segment blocks A and B are grouped inside brackets i.e. ‘(A AND B)’, then they are multiple segment blocks ‘nested’ together. So a combination of segment blocks represents the ‘nested blocks’. With the nested AND/OR capabilities, you can create complex segments with combinations such as:- (A AND B) OR (C AND D). Here, (A AND B) and (C AND D) are 2 nested blocks combined together with the OR operator.
- (A OR B) AND (C OR D). Here, (A OR B) and (C OR D) are 2 nested blocks combined together with the AND operator.
- (A OR B) AND C AND D. Here, (A OR B) is 1 nested block combined together with 2 other segment blocks C and D using the AND operator.
- (A AND B) OR C OR D. Here, (A AND B) is 1 nested block combined together with 2 other segment blocks C and D using the OR operator.

Understanding Nesting Levels
The examples mentioned above have 1 level of nesting. As in, for the complex segment (A AND B) OR (C AND D). In the above example A, B, C, and D have simple definitions using user attributes or event attributes. Those are not segments, those are simple user attributes or event attributes. But when the A, B, C, or D are in itself a nested block or a segment instead of simple definitions, they will have more than 1 level of nesting. Actions that increase the nesting level:- By using a custom segment in a query: If a custom segment is used in a query it increases the nesting level of query in below way:
[Nesting level of that query = nth level of custom segment + 1 Level increase] - By using a nested filter in a query: If a nested filter is used in a query it increases the nesting level of the query by 1 level

- Using the normal filter button to the query will not affect the nesting level. Any custom segment created using this logic will create a custom segment with nesting level 1.
- Adding multiple nested filters to the query will not add to the nesting level by subsequent levels. At least 1 nested filter is sufficient to increase the nesting level by 1.

Various cases of nesting levels:
| Custom segment | Definition | Nesting level |
|---|---|---|
| Type 1 | Created a segment using attributes (no nested filter) Or Created a segment using attributes and nested filter | Level 1 |
| Type 2 | Created a segment using only 1 custom segment of Level 1/Type 1 | Level 2 |
| Type 3 | Created a segment using 2 linked custom segments (only segments and no nested filters in their definition) - Eg: A -> B -> C, so C will be a Custom Segment of level 3 which has Custom Segment B (Level 2) and Custom Segment A (Level 1) nested/linked inside it. Or Created a segment using a custom segment of Level 1/Type 1 with at least 1 nested filter | Level 3 |
| Type 4 | 1 segment (of Level 2/Type 2) + 1 attribute outside with nested filter | More than 3 level of nesting |
| Type 5 | 2 linked segment inside + 1 attribute outside with nested filter | More than 3 level of nesting |
| Type 6 | Created using 4 linked segment | More than 3 level of nesting |
Let’s understand this using few examples:
Case 1: Level 1 Nesting
F is an event “Has executed App Inbox Icon Clicked atleast 1 time in the last 3 days” G is an event “Email viewed in Browser” H is an user attribute “Install status is True” I is an event “WhatsApp message clicked”
- Any custom segment created only using attributes or nested filter will create a custom segment of Level 1

Case 2: Level 2 Nesting
Let’s say, Segment F = Unsubscribe is False

- Nesting level of that query = nth level of custom segment + 1 Level increase
Case 3: Level 3 Nesting
Definition of segment: F is a custom segment made up of event attribute “Best time to send Email is between 10 Hrs and 13 Hrs. This will create a custom segment F of nesting level 1.

- Two level because a custom segment F (of nesting level 1) has been added in the query (Level 1 of custom segment F + 1 Level = Nesting Level 2)
- Another level - Using nested filter an event attribute is used that increases the nesting level to 2



Case 4: > Level 3 Nesting
Definition of segment: F is a custom segment made up of user or event attributes J, K, L, M and F is used as a custom segment in Segment E’s definition F is a nested segment defined as - (J OR K) AND (L OR M). J is an event “Has executed Notification Received Android predominantly with (Device Model is Nexus 5 (case insensitive) ) in the last 3 days” K is an user attribute “Best time to send Email is between 10 and 13” L is an user attribute “SPAM is false” M is an user attribute “Mobile user is True”
( {(J OR K) AND (L OR M)} AND G) OR (H AND I).

- Three levels because a custom segment F (of nesting level 2) has been introduced (Level 2 of custom segment F + 1 Level = Level 3) - as per rule mentioned in the beginning.
- Another level - Using nested filter an event attribute is used that increases the nesting level to 4

How to use nested filters
Use the ’+ Nested Filter’ button to add a nested block. To create a segment with more than one level of nesting, you will need to create a custom segment first (segment E in the above example) and then use the custom segment in the filters. The nested condition will be preselected, opposite to the outer condition. So, if the outer condition is AND, the nested condition will be OR (ex: (A AND B) OR (C AND D)) and vice-versa (ex: (A OR B) AND (C OR D)). Both the nested as well as the outer conditions can be changed. When the outer condition is changed (let’s say, from AND to OR), the nested condition automatically gets updated to the opposite condition (in this case, OR to AND) and vice versa. Nested AND/OR functionalities are available in segmentation as well as campaign creation pages in the target audience section. They are not supported in Trigger Criteria in Event-triggered campaigns. A particular user segment can also be excluded from the selected user segment. To use this, click on Exclude user checkbox. MoEngage supports up to 3 levels of nesting. These combinations offer flexibility in defining specific conditions for segmenting your users. Note:File Segments are not counted towards the nesting Limits.
How to simplify if there are more than 3 levels of nesting?
Segment queries are logical expressions of conditions. Any logical expression can be simplified into 1 level of nesting. Logical rules can be utilized to simplify it. Logical rules used to simplify the nesting -- (X AND Y) AND Z = X AND Y AND Z
- (X OR Y) OR Z = X OR Y OR Z
- (X AND Y) OR Z = (X OR Z) AND (Y OR Z)
- (X OR Y) AND Z = (X AND Z) OR (Y AND Z)
Then, the expanded definition of the segment - (C OR D) AND B Segment block C has another definition - C = E AND F
Then, the expanded definition of the segment - ((E AND F) OR D) AND B Simplifying this - (E OR D) AND (F OR D) AND B Segment block E has another definition - E = G AND H Then, the expanded definition of the segment - ((G AND H) OR D) AND (F OR D) AND B Finally, simplify the above expression in this way - (G OR D) AND (H OR D) AND (F OR D) AND B. This query will have nesting level as 1. With the above example, we can understand that any segment definition can be broken down into 1 level of nesting.