# Mautic segment filters

**URL:** <https://forum.mautic.org/t/mautic-segment-filters/30717>\
**Category:** Product Support\
**Created:** [January 30, 2024, 10:56am UTC](https://forum.mautic.org/t/mautic-segment-filters/30717 "2024-01-30T10:56:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrylis](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/astrylis/32/6421_2.png) [@astrylis](https://forum.mautic.org/u/astrylis)\
**Post date:** [January 30, 2024, 10:56am UTC](https://forum.mautic.org/t/mautic-segment-filters/30717/1 "2024-01-30T10:56:44Z")

</div>

**Your software**  
My Mautic version is:4.2.5  
My PHP version is:8.0  
My Database type and version is:MariaDB

**Your problem**  
My problem is: I want to be able to create a custom segment filter where they can search for a specific page\_hit and has an operator for the number of times. such as return a list of contacts that has page\_hit = [example.com](http://example.com) and has a count greater than 2.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [January 30, 2024, 9:05pm UTC](https://forum.mautic.org/t/mautic-segment-filters/30717/2 "2024-01-30T21:05:51Z")

</div>

Hi, this filter is not available in Mautic right now.

---

<div class="post-metadata">

**Author:** ![mzagmajster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mzagmajster/32/687_2.png) [@mzagmajster](https://forum.mautic.org/u/mzagmajster)\
**Post date:** [January 30, 2024, 9:24pm UTC](https://forum.mautic.org/t/mautic-segment-filters/30717/3 "2024-01-30T21:24:03Z")

</div>

No it does not, but mautic allows you to add custom filters via core events.

---

<div class="post-metadata">

**Author:** ![astrylis](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/astrylis/32/6421_2.png) [@astrylis](https://forum.mautic.org/u/astrylis)\
**Post date:** [February 1, 2024, 10:24am UTC](https://forum.mautic.org/t/mautic-segment-filters/30717/4 "2024-02-01T10:24:35Z")

</div>

This is probably not recommended but this is what I done. But I will share for others that want to try it out.

I create a new filter which is a direct copy of hit\_url\_count and named it to what I wanted to do.

```auto
'hit_url_x_count' => [
                'label' => 'Visited URL X Times',
                'properties' => ['type' => 'number'],
                'operators' => $this->typeOperatorProvider->getOperatorsIncluding([
                    OperatorOptions::EQUAL_TO,
                    OperatorOptions::GREATER_THAN,
                    OperatorOptions::LESS_THAN,
                    OperatorOptions::GREATER_THAN_OR_EQUAL,
                    OperatorOptions::LESS_THAN_OR_EQUAL,
                ]),
                'object' => 'lead',
            ],

```

In the ContactSegmentDictionary I added a new array for my new filter which again is close to the original hit\_url\_count except instead of field =\> id i used field =\> url.

```auto
$this->filters['hit_url_x_count'] = [
            'type' => ForeignFuncFilterQueryBuilder::getServiceId(),
            'foreign_table' => 'page_hits',
            'foreign_table_field' => 'lead_id',
            'table' => 'leads',
            'table_field' => 'id',
            'func' => 'count',
            'field' => 'url',
        ];

```

When I build my segment using the filter Visited X URL and then I use my new filter and it is working as expected. I can also add Visited URL (date) and filter by date too if needed,

Before I did this I did try to use Visit any URL x times and it did not do what I wanted. (had to try 🙂 )

Edit  
This is the query it ends up building

```auto
SELECT
    COUNT(leadIdPrimary) AS count,
    MAX(leadIdPrimary) AS maxId,
    MIN(leadIdPrimary) AS minId
FROM (
    SELECT DISTINCT
        l.id AS leadIdPrimary
    FROM
        leads l
    WHERE
        (
            l.id IN (
                SELECT par1.lead_id
                FROM page_hits par1
                WHERE par1.url = 'url here'
                            )
        )
        AND (
            EXISTS (
                SELECT COUNT(DISTINCT par3.url)
                FROM page_hits par3
                WHERE l.id = par3.lead_id
                HAVING COUNT(DISTINCT par3.url) >= 1
            )
        )
        AND (
            l.id IN (
                SELECT par5.lead_id
                FROM page_hits par5
                WHERE par5.date_hit >= "2024-01-31 15:00"
            )
        )
        AND (
            l.id NOT IN (
                SELECT par6.lead_id
                FROM lead_lists_leads par6
                WHERE par6.leadlist_id = 3735
            )
        )
) sss;

Excuse the hardcoded parameters it was part of my testing

```
