Skip to content

Commit 2fca086

Browse files
Update index.md
1 parent 1016f25 commit 2fca086

File tree

1 file changed

+18
-0
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+18
-0
lines changed

src/connections/sources/catalog/libraries/website/javascript/index.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,24 @@ Whenever the UTM parameters are no longer a part of the URL, Segment no longer i
841841
**Additional Note**:
842842
Segment does not validate UTM parameter names. This design supports the flexibility to track both standard parameters (e.g., utm_source, utm_medium) and custom parameters defined by users. As a result, all parameters present in the URL are collected as-is and added to the context field, without checks for naming conventions or validity.
843843
844+
If you want to ensure that only standard UTM parameters (e.g., utm_source, utm_medium, utm_campaign, utm_content, utm_term) are included in the context.campaign object, you can implement [Source middleware](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/middleware/) in your analytics.js setup. Here’s an example:
845+
846+
```js
847+
window.analytics.addSourceMiddleware(({ payload, next }) => {
848+
if (payload.obj.context?.campaign) {
849+
const allowedFields = ["source", "medium", "term", "campaign", "content"];
850+
const campaign = payload.obj.context.campaign;
851+
Object.keys(campaign).forEach(key => {
852+
if (!allowedFields.includes(key)) {
853+
delete campaign[key];
854+
}
855+
});
856+
}
857+
next(payload);
858+
});
859+
```
860+
This middleware will filter out any non-standard parameters from the context.campaign object before they are sent to Segment or forwarded to your enabled destinations.
861+
844862
## Analytics.js performance
845863
846864
The Analytics.js library and all Destination libraries are loaded with the [HTML script `async` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async){:target="_blank"}. This also means that Segment fires methods asynchronously, so you should adjust your code accordingly if you require that events be sent from the browser in a specific order.

0 commit comments

Comments
 (0)