Skip to content

Commit 214f53e

Browse files
fralongoFrancesco Longo
andauthored
feat: Allow analytics metadata component properties to be arrays (#147)
Co-authored-by: Francesco Longo <[email protected]>
1 parent bd4ad12 commit 214f53e

File tree

7 files changed

+12
-5
lines changed

7 files changed

+12
-5
lines changed

src/internal/analytics-metadata/__tests__/components.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const ComponentTwo = () => (
4545
);
4646

4747
export const ComponentThree = ({ children }: { children?: ReactNode }) => (
48-
<div {...getAnalyticsMetadataAttribute({ component: { name: 'ComponentThree' } })}>
48+
<div {...getAnalyticsMetadataAttribute({ component: { name: 'ComponentThree', properties: { arr: ['a', 'b'] } } })}>
4949
<div
5050
{...getAnalyticsMetadataAttribute({
5151
component: {
@@ -55,6 +55,7 @@ export const ComponentThree = ({ children }: { children?: ReactNode }) => (
5555
anotherLabel: { root: 'self' },
5656
yetAnotherLabel: { rootSelector: '.root-class-name' },
5757
},
58+
properties: { arr: ['c', 'd'] },
5859
},
5960
})}
6061
>

src/internal/analytics-metadata/__tests__/page-scanner-utils.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('getComponentsTree', () => {
5454
expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([
5555
{
5656
name: 'ComponentThree',
57+
properties: { arr: ['a', 'b'] },
5758
children: [
5859
{
5960
name: 'ComponentTwo',
@@ -76,6 +77,7 @@ describe('getComponentsTree', () => {
7677
expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([
7778
{
7879
name: 'ComponentThree',
80+
properties: { arr: ['a', 'b'] },
7981
children: [
8082
{
8183
name: 'ComponentTwo',
@@ -97,6 +99,7 @@ describe('getComponentsTree', () => {
9799
expect(getComponentsTree()).toEqual([
98100
{
99101
name: 'ComponentThree',
102+
properties: { arr: ['a', 'b'] },
100103
children: [
101104
{
102105
name: 'ComponentTwo',

src/internal/analytics-metadata/__tests__/testing-utils.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ describe('getRawAnalyticsMetadata', () => {
6363
rootSelector: '.root-class-name',
6464
},
6565
},
66+
properties: { arr: ['c', 'd'] },
6667
},
6768
},
6869
{
6970
component: {
7071
name: 'ComponentThree',
72+
properties: { arr: ['a', 'b'] },
7173
},
7274
},
7375
],

src/internal/analytics-metadata/__tests__/utils.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('getGeneratedAnalyticsMetadata', () => {
6666
type: 'component',
6767
detail: {
6868
name: 'ComponentThree',
69+
properties: { arr: ['a', 'b'] },
6970
innerContext: {
7071
position: '2',
7172
columnLabel: '',

src/internal/analytics-metadata/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface GeneratedAnalyticsMetadataComponent {
2323
instanceIdentifier?: string;
2424

2525
// relevant properties of the component. For example: {variant: 'primary'} for Button, {external: 'true', href: '...'} for Link
26-
properties?: Record<string, string>;
26+
properties?: Record<string, string | Array<string>>;
2727

2828
// relevant information on the specific area of the component in which the interaction happened
2929
innerContext?: Record<string, string>;

src/internal/analytics-metadata/metadata-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const processMetadata = (node: HTMLElement | null, localMetadata: any): G
2020
return Object.keys(localMetadata).reduce((acc: any, key: string) => {
2121
if (key.toLowerCase().match(/label$/)) {
2222
acc[key] = processLabel(node, localMetadata[key]);
23-
} else if (typeof localMetadata[key] !== 'string') {
23+
} else if (typeof localMetadata[key] !== 'string' && !Array.isArray(localMetadata[key])) {
2424
acc[key] = processMetadata(node, localMetadata[key]);
2525
} else {
2626
acc[key] = localMetadata[key];
@@ -45,7 +45,7 @@ export const merge = (inputTarget: any, inputSource: any): any => {
4545
merged[key] = target[key];
4646
} else if (!target[key] && !isNil(source[key])) {
4747
merged[key] = source[key];
48-
} else if (typeof target[key] === 'string') {
48+
} else if (typeof target[key] === 'string' || Array.isArray(target[key])) {
4949
merged[key] = source[key];
5050
} else {
5151
merged[key] = merge(target[key], source[key]);

src/internal/analytics-metadata/page-scanner-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getGeneratedAnalyticsMetadata } from './utils';
88
interface GeneratedAnalyticsMetadataComponentTree {
99
name: string;
1010
label: string;
11-
properties?: Record<string, string>;
11+
properties?: Record<string, string | Array<string>>;
1212
children?: Array<GeneratedAnalyticsMetadataComponentTree>;
1313
}
1414

0 commit comments

Comments
 (0)