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

fix: encode search params #24

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

NimaC
Copy link

@NimaC NimaC commented Jul 18, 2023

fixes #23

adds boolean flag to opts to enable encoding of search params

Prior

const payload = {
  cd_application_id: 'SST - Some Online Shop',
}

const payloadParsed = JSON.parse(JSON.stringify(payload))
const qs = new URLSearchParams(payloadParsed).toString()
console.log(decodeURIComponent(qs)) // cd_application_id=SST+-+Some+Online+Shop

Now

const payload = {
  cd_application_id: 'SST - Some Online Shop',
}

const opts = {
  encode_search_params: true,
}

const payloadParsed = JSON.parse(JSON.stringify(payload))
let qs = ''
if (opts.encode_search_params) {
  // Ensure spaces are not replaced with +
  qs = Object.entries(payloadParsed)
    .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
    .join('&')
} else {
  qs = new URLSearchParams(payloadParsed).toString()
}
console.log(decodeURIComponent(qs)) // cd_application_id=SST - Some Online Shop

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

Successfully merging this pull request may close these issues.

Adjust encoding of event payload to work in Google Analytics
1 participant