From 1016f2573cfdc52007e0b36a85c17ec1ad4f91fd Mon Sep 17 00:00:00 2001 From: prigiattiperrut <47340158+prigiattiperrut@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:59:17 -0300 Subject: [PATCH 1/3] Update index.md --- .../sources/catalog/libraries/website/javascript/index.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/connections/sources/catalog/libraries/website/javascript/index.md b/src/connections/sources/catalog/libraries/website/javascript/index.md index a247af0176..f62e0e9330 100644 --- a/src/connections/sources/catalog/libraries/website/javascript/index.md +++ b/src/connections/sources/catalog/libraries/website/javascript/index.md @@ -838,6 +838,9 @@ 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. + ## 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. From 2fca0861fd688c935726ec61aab139fefb374c38 Mon Sep 17 00:00:00 2001 From: prigiattiperrut <47340158+prigiattiperrut@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:04:36 -0300 Subject: [PATCH 2/3] Update index.md --- .../libraries/website/javascript/index.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/connections/sources/catalog/libraries/website/javascript/index.md b/src/connections/sources/catalog/libraries/website/javascript/index.md index f62e0e9330..45c9a5fb3f 100644 --- a/src/connections/sources/catalog/libraries/website/javascript/index.md +++ b/src/connections/sources/catalog/libraries/website/javascript/index.md @@ -841,6 +841,24 @@ Whenever the UTM parameters are no longer a part of the URL, Segment no longer i **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. +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: + +```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. + ## 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. From 8800330919ccc99c1e0c1279d3b08f4de5d778ef Mon Sep 17 00:00:00 2001 From: stayseesong <83784848+stayseesong@users.noreply.github.com> Date: Tue, 17 Dec 2024 15:53:22 -0800 Subject: [PATCH 3/3] Apply suggestions from code review --- .../catalog/libraries/website/javascript/index.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/connections/sources/catalog/libraries/website/javascript/index.md b/src/connections/sources/catalog/libraries/website/javascript/index.md index 45c9a5fb3f..44e3a40650 100644 --- a/src/connections/sources/catalog/libraries/website/javascript/index.md +++ b/src/connections/sources/catalog/libraries/website/javascript/index.md @@ -838,10 +838,11 @@ 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. +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. -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: +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. + +For example: ```js window.analytics.addSourceMiddleware(({ payload, next }) => { @@ -857,7 +858,7 @@ window.analytics.addSourceMiddleware(({ payload, next }) => { 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. +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. ## Analytics.js performance