Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Use EncodeUri() if part of the URL path or EncodeUriComponent() if used as a URL parameter #2

Open
semisight opened this issue Aug 12, 2022 · 3 comments

Comments

@semisight
Copy link

When reviewing your template we've noticed that it is using parameters to build a url. It's a best practice to use [EncodeUri()](https://developers.google.com/tag-manager/templates/api#encodeuri) if part of the URL path or [EncodeUriComponent()](https://developers.google.com/tag-manager/templates/api#encodeuricomponent) if used as a URL parameter.

@harmasz
Copy link
Contributor

harmasz commented Aug 16, 2022

Hey @semisight, we are currently using encodeUri() on the whole request path before running sendHttpGet(), like this:

// Build the full URL for sending the request
const url = data.accountURL + encodeUri(requestPath);
// The sendHttpGet API takes a URL and a result callback. You must call
// data.gtmOnSuccess() or data.gtmOnFailure() so that the container knows when
// the tag has finished executing.
sendHttpGet(url, (statusCode) => {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
});

Do you mean that we should switch to encoding each param with encodeUriComponent()?

@endgameapp
Copy link

endgameapp commented Aug 25, 2022

You should indeed change that behavior. For example, if a string will contain &-character it will break the query (since encodeUri will not encode & characters and thus will break up the parameter.

@endgameapp
Copy link

For example, when event.page_title = 'Cookies & Drinks' it will not encode & and thus break the URL parameters.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants