|
| 1 | +--- |
| 2 | +title: Apply filters |
| 3 | +weight: 7 |
| 4 | +--- |
| 5 | + |
| 6 | +# How to apply filters |
| 7 | + |
| 8 | +This guide explains how to add filters to existing declarations to remove meaningless content that cannot be removed with CSS selectors, to prevent noise in the versions. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- An existing terms declaration file. |
| 13 | +- Having already identified the noise to remove and having double-checked it cannot be removed with CSS selectors with the [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) property. |
| 14 | + |
| 15 | +## Step 1: Check for built-in filters |
| 16 | + |
| 17 | +Built-in filters are pre-defined functions that handle common noise patterns. They are the easiest way to clean up content. |
| 18 | + |
| 19 | +Review the available [built-in filters]({{< relref "/terms/reference/built-in-filters" >}}) to find if one matches your needs. |
| 20 | + |
| 21 | +If you find a suitable built-in filter, proceed to [Step 3](#step-3-declare-the-filter), otherwise you will need to create a custom filter. |
| 22 | + |
| 23 | +## Step 2: Create a custom filter _(optional)_ |
| 24 | + |
| 25 | +If no built-in filter matches your needs, you will need to create a custom filter. This requires JavaScript knowledge and familiarity with DOM manipulation. |
| 26 | + |
| 27 | +### Create the filter file |
| 28 | + |
| 29 | +Create a JavaScript file in the same folder and with the same name as your service declaration, but with `.filters.js` extension. |
| 30 | + |
| 31 | +> For example, if your declaration is `declarations/MyService.json`, create `declarations/MyService.filters.js`. |
| 32 | +
|
| 33 | +### Write the filter function |
| 34 | + |
| 35 | +Define your filter function with the following signature: |
| 36 | + |
| 37 | +```js |
| 38 | +export function myCustomFilter(document, [parameters]) { |
| 39 | + // Your filter logic here |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +#### Parameters |
| 44 | + |
| 45 | +- `document`: JSDOM document instance representing the web page |
| 46 | +- `parameters`: values passed from the declaration _(optional)_ |
| 47 | + |
| 48 | +#### Example: Remove session IDs from text content |
| 49 | + |
| 50 | +For example, let's say you want to remove session IDs from text content: |
| 51 | + |
| 52 | +```html |
| 53 | +<p>We collect your data for the following purposes:</p> |
| 54 | +<ul> |
| 55 | + <li>To provide our services</li> |
| 56 | + <li>To improve user experience</li> |
| 57 | +</ul> |
| 58 | +<p class="session-id">Last updated on 2023-12-07 (Session: abc123def456)</p> |
| 59 | +``` |
| 60 | + |
| 61 | +You can implement this filter as follows: |
| 62 | + |
| 63 | +```js |
| 64 | +export function removeSessionIds(document) { |
| 65 | + // Find all paragraphs that might contain session IDs |
| 66 | + const paragraphs = document.querySelectorAll('.session-id'); |
| 67 | + |
| 68 | + paragraphs.forEach(paragraph => { |
| 69 | + let text = paragraph.textContent; |
| 70 | + // Remove session ID patterns like "Session: abc123" or "(Session: def456)" |
| 71 | + text = text.replace(/\s*\(?Session:\s*[a-zA-Z0-9]+\)?/g, ''); |
| 72 | + paragraph.textContent = text.trim(); |
| 73 | + }); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Result after applying the filter: |
| 78 | + |
| 79 | +```diff |
| 80 | + <p>We collect your data for the following purposes:</p> |
| 81 | + <ul> |
| 82 | + <li>To provide our services</li> |
| 83 | + <li>To improve user experience</li> |
| 84 | + </ul> |
| 85 | +- <p class="session-id">Last updated on 2023-12-07 (Session: abc123def456)</p> |
| 86 | ++ <p class="session-id">Last updated on 2023-12-07</p> |
| 87 | +``` |
| 88 | + |
| 89 | +## Step 3: Declare the filter |
| 90 | + |
| 91 | +Open your service declaration file (e.g. `declarations/MyService.json`) and locate the `filter` property of the specific terms you want to apply the filter to. If it doesn't exist, add it as an array. |
| 92 | + |
| 93 | +### Filter without parameters |
| 94 | + |
| 95 | +For filters that don’t require parameters, add the filter name as a string: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "name": "MyService", |
| 100 | + "terms": { |
| 101 | + "Privacy Policy": { |
| 102 | + "fetch": "https://my.service.example/en/privacy-policy", |
| 103 | + "select": ".textcontent", |
| 104 | + "filter": [ |
| 105 | + "removeSessionIds" |
| 106 | + ] |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Filter with parameters |
| 113 | + |
| 114 | +For filters that take parameters, use an object format, for example with the built-in filter `removeQueryParams` to remove query parameters from URLs: |
| 115 | + |
| 116 | +```json |
| 117 | +{ |
| 118 | + "name": "MyService", |
| 119 | + "terms": { |
| 120 | + "Privacy Policy": { |
| 121 | + "fetch": "https://my.service.example/en/privacy-policy", |
| 122 | + "select": ".textcontent", |
| 123 | + "filter": [ |
| 124 | + { |
| 125 | + "removeQueryParams": ["utm_source", "utm_medium", "utm_campaign"] |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Multiple filters |
| 134 | + |
| 135 | +You can combine multiple filters in the same declaration: |
| 136 | + |
| 137 | +```json |
| 138 | +{ |
| 139 | + "name": "MyService", |
| 140 | + "terms": { |
| 141 | + "Privacy Policy": { |
| 142 | + "fetch": "https://my.service.example/en/privacy-policy", |
| 143 | + "select": ".textcontent", |
| 144 | + "filter": [ |
| 145 | + { |
| 146 | + "removeQueryParams": ["utm_source", "utm_medium"] |
| 147 | + }, |
| 148 | + "removeSessionIds" |
| 149 | + ] |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## Step 4: Test the filter |
| 156 | + |
| 157 | +After adding the filter, test your declaration to ensure it works correctly: |
| 158 | + |
| 159 | +1. Start the terms tracking process |
| 160 | +2. Check that the noise has been removed |
| 161 | +3. Verify that important content is preserved |
0 commit comments