Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a note on UTM parameters validation #7316

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,27 @@ So, for example, if somebody follows the link with above query string to your si

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.

**Additional Note**:
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.
stayseesong marked this conversation as resolved.
Show resolved Hide resolved

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:
stayseesong marked this conversation as resolved.
Show resolved Hide resolved

```js
window.analytics.addSourceMiddleware(({ payload, next }) => {
if (payload.obj.context?.campaign) {
const allowedFields = ["source", "medium", "term", "campaign", "content"];
const campaign = payload.obj.context.campaign;
Object.keys(campaign).forEach(key => {
if (!allowedFields.includes(key)) {
delete campaign[key];
}
});
}
next(payload);
});
```
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.
stayseesong marked this conversation as resolved.
Show resolved Hide resolved

## Analytics.js performance

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.
Expand Down