From b4be53a07f4a3fb1a7c2a026f96b1d6c8d1c4f47 Mon Sep 17 00:00:00 2001
From: Peter Perlepes
Date: Mon, 22 May 2023 16:15:53 +0300
Subject: [PATCH] Round dimension type values on payload (close #1195)
---
...idth-x-height-values_2023-05-22-13-17.json | 10 +++++++
...idth-x-height-values_2023-05-22-13-17.json | 10 +++++++
.../src/helpers/browser_props.ts | 27 +++++++++++++++----
.../test/browser_props.test.ts | 13 +++++++++
libraries/tracker-core/src/core.ts | 4 +--
5 files changed, 57 insertions(+), 7 deletions(-)
create mode 100644 common/changes/@snowplow/browser-tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
create mode 100644 common/changes/@snowplow/tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
create mode 100644 libraries/browser-tracker-core/test/browser_props.test.ts
diff --git a/common/changes/@snowplow/browser-tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json b/common/changes/@snowplow/browser-tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
new file mode 100644
index 000000000..78e5715da
--- /dev/null
+++ b/common/changes/@snowplow/browser-tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
@@ -0,0 +1,10 @@
+{
+ "changes": [
+ {
+ "packageName": "@snowplow/browser-tracker-core",
+ "comment": "Round dimension type values on payload",
+ "type": "none"
+ }
+ ],
+ "packageName": "@snowplow/browser-tracker-core"
+}
\ No newline at end of file
diff --git a/common/changes/@snowplow/tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json b/common/changes/@snowplow/tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
new file mode 100644
index 000000000..d3b2b0269
--- /dev/null
+++ b/common/changes/@snowplow/tracker-core/feature-1195-round-width-x-height-values_2023-05-22-13-17.json
@@ -0,0 +1,10 @@
+{
+ "changes": [
+ {
+ "packageName": "@snowplow/tracker-core",
+ "comment": "Make addPayloadPair more lenient with 'uknown' type",
+ "type": "none"
+ }
+ ],
+ "packageName": "@snowplow/tracker-core"
+}
\ No newline at end of file
diff --git a/libraries/browser-tracker-core/src/helpers/browser_props.ts b/libraries/browser-tracker-core/src/helpers/browser_props.ts
index a59acd2cb..4b3a66d15 100644
--- a/libraries/browser-tracker-core/src/helpers/browser_props.ts
+++ b/libraries/browser-tracker-core/src/helpers/browser_props.ts
@@ -1,8 +1,11 @@
+/* Separator used for dimension values e.g. widthxheight */
+const DIMENSION_SEPARATOR = 'x';
+
export function getBrowserProperties() {
return {
- viewport: detectViewport(),
- documentSize: detectDocumentSize(),
- resolution: screen.width + 'x' + screen.height,
+ viewport: floorDimensionFields(detectViewport()),
+ documentSize: floorDimensionFields(detectDocumentSize()),
+ resolution: floorDimensionFields(detectScreenResolution()),
colorDepth: screen.colorDepth,
devicePixelRatio: window.devicePixelRatio,
cookiesEnabled: window.navigator.cookieEnabled,
@@ -35,7 +38,7 @@ function detectViewport() {
}
if (width >= 0 && height >= 0) {
- return width + 'x' + height;
+ return width + DIMENSION_SEPARATOR + height;
} else {
return null;
}
@@ -55,5 +58,19 @@ function detectDocumentSize() {
bodyHeight = be ? Math.max(be.offsetHeight, be.scrollHeight) : 0;
var w = Math.max(de.clientWidth, de.offsetWidth, de.scrollWidth);
var h = Math.max(de.clientHeight, de.offsetHeight, de.scrollHeight, bodyHeight);
- return isNaN(w) || isNaN(h) ? '' : w + 'x' + h;
+ return isNaN(w) || isNaN(h) ? '' : w + DIMENSION_SEPARATOR + h;
+}
+
+function detectScreenResolution() {
+ return screen.width + DIMENSION_SEPARATOR + screen.height;
+}
+
+export function floorDimensionFields(field?: string | null) {
+ return (
+ field &&
+ field
+ .split(DIMENSION_SEPARATOR)
+ .map((dimension) => Math.floor(Number(dimension)))
+ .join(DIMENSION_SEPARATOR)
+ );
}
diff --git a/libraries/browser-tracker-core/test/browser_props.test.ts b/libraries/browser-tracker-core/test/browser_props.test.ts
new file mode 100644
index 000000000..73a456e51
--- /dev/null
+++ b/libraries/browser-tracker-core/test/browser_props.test.ts
@@ -0,0 +1,13 @@
+import { floorDimensionFields } from '../src/helpers/browser_props';
+
+describe('Browser props', () => {
+ it('floorDimensionFields correctly floors dimension type values', () => {
+ const testDimensions = '100x100';
+ expect(floorDimensionFields(testDimensions)).toEqual(testDimensions);
+ });
+
+ it('floorDimensionFields correctly floors dimension type values with fractional numbers', () => {
+ const testFractionalDimensions = '100.2x100.1';
+ expect(floorDimensionFields(testFractionalDimensions)).toEqual('100x100');
+ });
+});
diff --git a/libraries/tracker-core/src/core.ts b/libraries/tracker-core/src/core.ts
index 5cc88f883..4a37e7f16 100644
--- a/libraries/tracker-core/src/core.ts
+++ b/libraries/tracker-core/src/core.ts
@@ -155,7 +155,7 @@ export interface TrackerCore {
* @param key - Field name
* @param value - Field value
*/
- addPayloadPair: (key: string, value: string | number) => void;
+ addPayloadPair: (key: string, value: unknown) => void;
/**
* Get current base64 encoding state
@@ -429,7 +429,7 @@ export function trackerCore(configuration: CoreConfiguration = {}): TrackerCore
* @param key - Field name
* @param value - Field value
*/
- function addPayloadPair(key: string, value: string | number): void {
+ function addPayloadPair(key: string, value: unknown): void {
payloadPairs[key] = value;
}