Skip to content

Commit af0565b

Browse files
committed
Update docs/custom-query-params.md
1 parent 50dd9db commit af0565b

File tree

1 file changed

+13
-42
lines changed

1 file changed

+13
-42
lines changed

docs/custom-query-params.md

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,23 @@ Other than the source attribution query parameters listed above, Plausible strip
88

99
If you still want some pages to be reported with the complete URL that includes the query part, here's what you should do:
1010

11-
## 1. Add the `manual` script extension
11+
## 1. Disable automatic pageviews
1212

13-
You can use Plausible's manual script extension and define a custom location for all such pageviews.
13+
To specify a custom location for your event, you must disable automatic pageview tracking. Update your snippet to set `autoCapturePageviews` to `false`.
1414

15-
To do so, change your Plausible script snippet `src` attribute from `https://plausible.io/js/script.js` to `https://plausible.io/js/script.manual.js`.
16-
17-
The new snippet will look like this (make sure to change the `data-domain` attribute to the domain you added to your Plausible account):
18-
19-
```html
20-
<script defer data-domain="yourdomain.com" src="https://plausible.io/js/script.manual.js"></script>
15+
```javascript
16+
plausible.init({
17+
autoCapturePageviews: false
18+
})
2119
```
22-
Do this on all the pages where you want to track the custom query parameters.
2320

24-
## 2. Trigger the pageview event with a custom `u` attribute
21+
Do this on all the pages where you want to track the custom query parameters.
2522

26-
Once you've started using the manual extension, the `pageview` event won't be sent automatically. This means that the stats won't be tracked anymore by default. You'll have to trigger the events to be counted manually.
23+
## 2. Trigger the pageview event with a custom `url` attribute
2724

28-
To trigger events manually, you need to add the following script after your regular Plausible tracking snippet:
25+
Once you've disabled `autoCapturePageviews` extension, the `pageview` event won't be sent automatically. This means that the stats won't be tracked anymore by default. You'll have to trigger the events to be counted manually.
2926

30-
```html
31-
<!-- define the `plausible` function to manually trigger events -->
32-
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
33-
```
34-
35-
Once that's done, you can create another script in which you will trigger your pageview event. This is where you can also define a custom location and
27+
Now you can create another script in which you will trigger your pageview event. This is where you can also define a custom location and
3628
make the query part of the URL look like a standard subfolder. To do so, add the following snippet:
3729

3830
```html
@@ -48,42 +40,21 @@ make the query part of the URL look like a standard subfolder. To do so, add the
4840
}
4941
return customUrl
5042
}
51-
plausible('pageview', { u: prepareUrl(["CUSTOM_PARAM_1", "CUSTOM_PARAM_2", ... ]) + window.location.search })
43+
plausible('pageview', { url: prepareUrl(["CUSTOM_PARAM_1", "CUSTOM_PARAM_2", ... ]) + window.location.search })
5244
</script>
5345
```
5446
Make sure to replace `CUSTOM_PARAM_X` with your query parameter names. You can define as many as you want.
5547
For example, for `yoursite.com/blog/index.php?article=some_article&page=11` you could write the last line as
5648

5749
```javascript
58-
plausible('pageview', { u: prepareUrl(["article", "page"]) + window.location.search });
50+
plausible('pageview', { url: prepareUrl(["article", "page"]) + window.location.search });
5951
```
6052

6153
and the page path will be reported to your dashboard as `blog/index.php/some_article/11`
6254

63-
At this point, your entire setup should look like this:
64-
65-
```html
66-
<script defer data-domain="yourdomain.com" src="https://plausible.io/js/script.manual.js"></script>
67-
<!-- define the `plausible` function to manually trigger events -->
68-
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
69-
<!-- trigger pageview -->
70-
<script>
71-
function prepareUrl(params) {
72-
const url = new URL(location.href)
73-
const queryParams = new URLSearchParams(location.search)
74-
let customUrl = url.protocol + "//" + url.hostname + url.pathname.replace(/\/$/, '')
75-
for (const paramName of params) {
76-
const paramValue = queryParams.get(paramName)
77-
if (paramValue) customUrl = customUrl + '/' + paramValue
78-
}
79-
return customUrl
80-
}
81-
plausible('pageview', { u: prepareUrl(["CUSTOM_PARAM_1", "CUSTOM_PARAM_2", ... ]) + window.location.search })
82-
</script>
83-
```
8455

8556
:::note
8657
The `+ window.location.search` is needed to persist source acquisition query parameters from your actual URL. Plausible uses `ref`, `source`, `utm_source`, `utm_medium`, `utm_campaign`, `utm_content` and `utm_term` query parameters for source acquisition.
8758
:::
8859

89-
That's it! You're now tracking the complete URLs of the pages that include custom parameters.
60+
That's it! You're now tracking the complete URLs of the pages that include custom parameters.

0 commit comments

Comments
 (0)