snowplow-javascript-tracker
snowplow-javascript-tracker copied to clipboard
Make sure arrays retrieved from browser APIs are serialized as arrays in context entities
Describe the bug
The client hints plugin can produce invalid http_client_hints
context entity due to assigning the brands
array as an object in case browsers return such value from navigator.userAgentData.brands
. This is unexpected behaviour from the browser but does seem to happen in some cases.
For example, the brands property may look like this: "brands":{"0":{"brand":" Not A;Brand","version":"99"}}
instead of "brands":[{"brand":" Not A;Brand","version":"99"}]
.
To Reproduce Unfortunately, I can't provide clear instructions on how to reproduce this as it only happens under certain cases. However, we do have examples of such badly formed events.
Expected behavior
The brands
property should be an array instead of the object.
Suggested solution
We should process the value returned from navigator.userAgentData.brands
into an array to make sure that it is an array, e.g. using:
let brands = Object.keys(navigator.userAgentData.brands).map(function(e) {
return navigator.userAgentData.brands[e];
})
There might be other cases where the same problem occurs. So we should also check for those.
Thanks for creating the issue @matus-tomlein! For the record an excerpt from the most recent failed event.
- User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
-
"data":{ "isMobile":false, "brands":{ "0":{"brand":" Not A;Brand","version":"99"}, "1":{"brand":"Chromium","version":"102"}, "2":{"brand":"Google Chrome","version":"102"} } }
Failed event message excerpt:
{ "schema": "iglu:com.snowplowanalytics.snowplow.badrows/schema_violations/jsonschema/2-0-0", "data": { "processor": { "artifact": "snowplow-enrich-pubsub", "version": "3.1.5" }, "failure": { "timestamp": "2022-06-21T19:14:32.300320Z", "messages": [ { "schemaKey": "iglu:org.ietf/http_client_hints/jsonschema/1-0-0", "error": { "error": "ValidationError", "dataReports": [ { "message": "$.brands: object found, array expected", "path": "$.brands", "keyword": "type", "targets": [ "object", "array" ] } ] } } ] } ...