Skip to content

Commit 796a4e7

Browse files
committed
Add how to guide to apply filters
1 parent 65d3606 commit 796a4e7

File tree

1 file changed

+160
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)