Marketers usually segment users based on multiple criteria such as User Property rules (User Attributes), User activity rules (Event Attributes), User Affinity rules, and Custom Segments. Most of the time, a combination of multiple criteria using AND or OR conditions is used to create a complex user segment.

For better understanding, let’s say A, B, C, and D represent the ‘segment blocks’, which are events & user attributes with all their filters. For example, A can be ‘users who are male’, B can be ‘users who added to cart: wallet in the last 3 days, and so on. You can always create segments like A AND B AND C AND D and A OR B OR C OR D by combining segment blocks using AND/OR operators.

But when a complex segment like ‘(users who are male AND added to the cart: wallet in the last 3 days), OR (users who are female AND added to cart: handbag in the last 3 days)’ is required, it cannot be simply done using just these operators. The reason being either AND, or OR, only one of these will be used to combine these segment blocks. A combination of AND as well as OR cannot be used together. If you change one of the operators in the definition, the rest will automatically change. This is when nesting comes into the picture.

**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), A, B, C, and D have simple definitions using user attributes or event attributes.

But when the A, B, C, or D are in itself a nested block instead of simple definitions, they will have more than 1 level of nesting.

Let’s understand using an example:

Segment E has a definition as - **(F AND G) OR (H AND I).**

Hence E is a segment made of a definition that has one level of nesting.

Now let's say F is also a nested segment defined as - **(J OR K) AND (L OR M)****. **

In this case, segment E has 2 levels of nesting E -> F -> J.

So the overall segment E is now defined as - **( ****{(J OR K) AND (L OR M)} AND G) OR (H AND I).**

And now let’s say J is also a nested segment defined as -** (N AND O) OR (P AND Q).**

In this case, segment A has 3 levels of nesting E -> F -> J -> N.

So the overall segment E is now defined as -**( ****{( {(N AND O) OR (P AND Q)} OR K) AND (L OR M)} AND G) OR (H AND I).**

**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.

**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)

Here is an example of making a multi-nested segment into 1 level of the nested segment:

Let's say we have a segment block of A & B with a condition - A AND B

Segment block A has another definition - A = C OR D

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

Simplifying this - (G OR D) AND (H OR D) AND (F OR D) AND B

With the above example, we can understand that any segment definition can be broken down

into 1 level of nesting.