# General setup

{% hint style="warning" %}
To configure Contextual targeting feature (contextual extraction & semantic analysis), please follow the next steps **IN ORDER**:

1. Schema update
2. Event rule configuration
   {% endhint %}

## Schema update

{% hint style="warning" %}
Contextual targeting only works as of now with **standard schema**. Please reach out to your Account manager if you do not have **UserEvent** type in your schema.
{% endhint %}

For *Panel-based* as well as *Semantic contextual targeting*, you'll need to update your runtime schema to add the following line in **UserEvent** type:

```
contextual_key:String @TreeIndex(index:"USER_INDEX")
```

{% hint style="warning" %}
**$url** attribute needs to be populated with a proper URL in order to extract the contextual\_key (URL without protocol nor query stings).
{% endhint %}

As well, ensure that the field **ts** in **UserEvent** is indexed:

```
ts:Timestamp! @TreeIndex(index:"USER_INDEX")
```

For *Semantic contextual targeting*, and particularly if you want to create *Audience segments* based on semantic information, you'll need to add the following lines in **UserEvent** type:

```
semantic_tagging:[SemanticTagging] @Property(path:"$properties.$semantic_tagging")
targeting_list_ids:[String] @TreeIndex(index:"USER_INDEX") @ReferenceTable(type:"CORE_OBJECT", model_type:"CONTEXTUAL_TARGETING_LISTS") @Property(path:"$properties.$targeting_list_ids")
```

As well as the following types:

```
type SemanticTagging  {
   provider_id:String @Property(path:"$provider_id") @TreeIndex(index:"USER_INDEX")
   provider_version:String @Property(path:"$provider_version") @TreeIndex(index:"USER_INDEX")

   entities:[SemanticEntity] @Property(path:"$entities")
   iab_categories:[SemanticIABCategory] @Property(path:"$iab_categories")
}

type SemanticEntity  {
   entity_id:String @Property(path:"$entity_id") @TreeIndex(index:"USER_INDEX")
   name:String @Property(path:"$name") @TreeIndex(index:"USER_INDEX")
   type:String @Property(path:"$type")
   wikidata_id:String @Property(path:"$wikidata_id")
}

type SemanticIABCategory  {
   category_id:String @TreeIndex(index:"USER_INDEX") @Property(path:"$category_id")
   name:String @TreeIndex(index:"USER_INDEX") @Property(path:"$name")
}
```

{% hint style="warning" %}
Don't forget to add **@EdgeAvailability** on **semantic\_tagging** & **targeting\_list\_ids** properties if you want to use those characteristics for building Edge segments. [More information about Edge segments](https://developer.mediarithmics.io/advanced-usages/audiences/edge-segments).
{% endhint %}

## Event rule configuration

You need to add **Contextual Targeting Extractor** [event rule](https://developer.mediarithmics.io/data-streams/data-ingestion/real-time-user-tracking/event-rules) to channels on which you want to use the contextual targeting feature. This event rule will have various objectives:   &#x20;

* Specify which JavaScript tag will be responsible for URL & hit collection:
  * **User event tag**: uses the URL explicitly passed in the event. In that case, you will need to install the [tracking snippet](https://developer.mediarithmics.io/data-streams/data-ingestion/real-time-user-tracking/web-events#implementing-the-snippet).
  * **Contextual targeting tag**: automatically captures the page URL from the browser. In that case, you will need to install the [contextual snippet](https://developer.mediarithmics.io/advanced-usages/activation-setup#contextual-snippet-installation).
* Calculate and add the contextual key in the event. This is done automatically when adding the event rule.
* Add the semantic information extracted from a URL in the event . This is required if you want to create *Audience segments* based on semantic information. Check **Tag event with semantic extract** in that case
* Specify URLs (Blacklist URLs) or group of URLs (Blacklist RegEx) to blacklist. No semantic extraction nor targeting will be perfomed on those URLs. If we take the example of a news website, we recommend blacklisting pages that list articles, usually related to the same theme (e.g. "politics") and that are frequently refreshed.

### Blacklist URL

Add a URL to blacklist per line. ALL URLs provided should NOT have protocol in it (`http://`, `https://`). For example:

```
www.mediarithmics.io/homepage
actu.mediarithmics.io/
mediarithmics.io/actu
```

### Blacklist Regexp

Add a regexp to blacklist per line. Note that only \* is authorized and \* will automatically be applied before and after the input regexp. For example:

```
mediarithmics*.io/homepage
```

Will blacklist the following URLs:

* [www.mediarithmics.io/homepage](http://www.mediarithmics.io/homepage)
* mediarithmicsTEST.io/homepage
* mediarithmics\*io/homepage/page.
