# Event rules

**Every time an event is sent, we run the event rules associated with its channel**. They are **predefined actions** to extract properties, reshape data or identify a user from a property.

{% hint style="success" %}
If the action you wish to do on each event is not possible with an event rule, you should have a look at [Activity Analyzers](/data-streams/data-ingestion/real-time-user-tracking/activity-analyzers.md).
{% endhint %}

## How to add event rules

You can go to a **channel's settings, edit, and then scroll down to event rules.**

![](/files/-MPiSHy2PHO3YYQduz-D)

## Available event rules

### Property To Origin Copy

This Event Rule will help you take an existing property from an event and copy it into the origin of the activity. You can copy:

* The URL of the Event
* The Referrer of the Event
* Any Event Property

### Url Match

This Event Rule allows you to write a **pattern** that will be **matched against all the $url values of incoming $page\_view events**. The pattern can extract some values from the Url Path and from the Url Query String.&#x20;

*If you want more information about the different parts of an URL,* [*please read this article.*](https://en.wikipedia.org/wiki/URL)

#### Limitations

Url Match only works for $page\_view events. Any other event won't be processed by the Url Match event rule.

{% hint style="warning" %}
Please note that $page\_view events will be deleted at the end of the activity processing stage.
{% endhint %}

#### Use case

Useful if for technical and/or organizational reasons, you can't customize the Tracking JS Snippet on a web page to retrieve information from the data layer.&#x20;

1. Use the [default JS Snippet ](/data-streams/data-ingestion/real-time-user-tracking/web-events.md#implementing-the-tag)that will automatically track $page\_view events with the URL of the page.
2. Use the Url Match event rule to extract properties from the page's URL and add them to the event.&#x20;

For example, if a $page\_view event is tracked with the URL `https://foo.bar/category/0001/article/super-awesome-article`, you would be able to generate this kind of event

```javascript
{
    "$ts": 1568296040000,
    "$event_name": "article_view",
    "$properties": {
        "category": "0001",
        "article_id": "super-awesome-article",
        "source": "web"
    }
}
```

#### How to

The URL Match is taking 2 parameters:

1. The URL Pattern that will be used to match the $url value of $page\_view events
2. The event template that will be used to generate the event if the $url is matching the URL pattern

**Patterns** are URL in which you can add:

* A variable extraction rule for path parameters with:variableName
* A wildcard with \*

{% hint style="info" %}
Wildcards can be placed several times in the URL pattern. For instance, you can do `https://foo.bar/*.*` which will match for a route shaped as `https://foo.bar/a/b/c.xls`
{% endhint %}

Example   `*//foo.bar/category/:categoryId/article/:articleId`

* \* at the beginning will match both URLs in and in http in the URL
* :categoryId will extract the value in the URL corresponding to the categoryId. As it is a named variable, it can be used as a value in the event that will be generated.
* :articleId works as:categoryId

**The Query String values are automatically extracted** in variables that have the name of the parameter in the Query String. `https://foo.bar/a/b/c?var=value&var2=value2` extracts both value and value2 in variables named var and var2.

The **event template** is containing the following information:

* The $event\_name that will be used for the generated event as a static string
* A list of properties in a key-value way that will be added in the event properties

In the properties values you can either pass:

* A static string directly. Ex: sport
* A string in the format {{variableName}} that will be replaced by a value extracted in the URL pattern

{% hint style="info" %}
If a variable used in the event template is not extracted from the URL (either from the path or from the Query String), it's value will be {{variableName}} in the generated event.
{% endhint %}

#### Examples

Let's take the following event rule:&#x20;

* Url pattern: `*//foo.bar/category/:categoryId/article/:articleId`
* Event template:

| Property            | Value          |
| ------------------- | -------------- |
| article\_id         | {{articleId}}  |
| category\_id        | {{categoryId}} |
| visit\_origin       | {{origin}}     |
| source\_event\_rule | 1              |

\
$page\_view event with Url: `https://foo.bar/category/0001/article/super-awesome-article?origin=email` will generate

```javascript
{
    "$ts": 1568296040000,
    "$event_name": "article_view",
    "$properties": {
        "category": "0001",
        "article_id": "super-awesome-article",
        "visit_origin": "email",
        "source_event_rule": "1"
    }
}
```

\
$page\_view event with Url `http://foo.bar/category/0001/article/super-awesome-article` will generate

```javascript
{
    "$ts": 1568296040000,
    "$event_name": "article_view",
    "$properties": {
        "category": "0001",
        "article_id": "super-awesome-article",
        "visit_origin": "{{origin}}",
        "source_event_rule": "1"
    }
}
```

$page\_view event with Url `https://oof.bar/category/0001/article/super-awesome-article` will generate *nothing* as the Url domain (oof.bar) is not matching the pattern.

page\_view event with Url `https://foo.bar/category/0001/article/super-awesome-article` will generate *nothing* as the "source" event name is not $page\_view.<br>

### User Identifier Insertion

This event rule allows you to **extract a property from an event to convert it into a user identifier,** such as an Email Hash or a User account ID.&#x20;

You can apply a hash on this extraction. We currently support the following hash methods:

* SHA\_256
* SHA\_1
* SHA\_384
* SHA\_512
* MD5
* MD2

This event rule is useful when you don't want to pass an identifier as a global property of the activity, but rather have it computed directly by your datamart.&#x20;

*If several values of an identifier are found within an activity, we keep only the last one.*

```javascript
{
    "$event_name": "PageView",
    "$properties": {
        "user_id": "<USER_ID>"
    }
}
```

In the scenario above, you will be able to extract the user\_id, store it as a User account ID.

### Contextual Targeting Extractor

This event rule is used for Contextual targeting purpose. You can find more information about it in the [Contextual targeting > Setup](/advanced-usages/contextual-targeting/general-setup.md#event-rule) section&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.mediarithmics.io/data-streams/data-ingestion/real-time-user-tracking/event-rules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
