Skip to content

Commit cec85ac

Browse files
authored
Merge pull request #7316 from segmentio/prigiattiperrut-patch-6
Adding a note on UTM parameters validation
2 parents 493ba96 + 8800330 commit cec85ac

File tree

1 file changed

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

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,28 @@ So, for example, if somebody follows the link with above query string to your si
838838
839839
Whenever the UTM parameters are no longer a part of the URL, Segment no longer includes them. For example, if the user goes to a new page within your website which does not contain these parameters, they will not be included in subsequent events. UTM parameters are non-persistent by default as they could potentially cause data accuracy problems. Here's an example of why: Say a user clicks on an ad and lands on your site. He navigates around and bookmarks an internal page - or maybe shares a link with a friend, who shares it with another friend. All those links would then point back to the same test utm_source as the initial referrer for any purchase.
840840
841+
Segment doesn't validate UTM parameter names. This design supports the flexibility to track both standard parameters (for example, utm_source, utm_medium) and custom parameters defined by users. As a result, all parameters present in the URL collected as is, and are added to the context field without checks for naming conventions or validity.
842+
843+
If you want to ensure that only standard UTM parameters (such as, utm_source, utm_medium, utm_campaign, utm_content, utm_term) are included in the context.campaign object, you can implement [Source middleware](/docs/connections/sources/catalog/libraries/website/javascript/middleware/) in your Analytics.js setup.
844+
845+
For example:
846+
847+
```js
848+
window.analytics.addSourceMiddleware(({ payload, next }) => {
849+
if (payload.obj.context?.campaign) {
850+
const allowedFields = ["source", "medium", "term", "campaign", "content"];
851+
const campaign = payload.obj.context.campaign;
852+
Object.keys(campaign).forEach(key => {
853+
if (!allowedFields.includes(key)) {
854+
delete campaign[key];
855+
}
856+
});
857+
}
858+
next(payload);
859+
});
860+
```
861+
This middleware filters out any non-standard parameters from the `context.campaign` object before they're sent to Segment or forwarded to your enabled destinations.
862+
841863
## Analytics.js performance
842864
843865
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)