When using the Analytics API, the tags
query parameter which declares the Analytics tags used to segment data must be URL-encoded.
When passing multiple values to the tags
query parameter, you combine them using an AND/OR approach, such as tag1 AND tag2
. Each tag must be URL-encoded individually to differentiate between:
- spaces within a tag value
- spaces between tags
This results in:
${encodeURIComponent(tag1)} AND ${encodeURIComponent(tag2)}
However, this value still contains spaces around the AND, meaning that it is not a valid query parameter. To resolve this, the entire tags
parameter value (i.e. tags=${...}
) must be URL-encoded again.
After this step, the spaces between the tags will appear as +
or %20
(plus characters (+
) are interpreted as spaces), making it valid.
The final encoded parameter structure is as follows:
For multiple tags:
?tags=${encodeURIComponent(`${encodeURIComponent(tag1)} AND ${encodeURIComponent}`)}
For a single tag:
?tags=${encodeURIComponent(`${encodeURIComponent(tag)}`)}
In essence, each tag is double-encoded. First, when encoding individual tag values and second when encoding the full query parameter. Failing to apply the double encoding leads to invalid queries.