Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit b666cd1

Browse files
Add support for analytics tracking Id in the backend (#858)
1 parent 79e7483 commit b666cd1

File tree

12 files changed

+42
-31
lines changed

12 files changed

+42
-31
lines changed

backend/postman/Performance Dashboard API.postman_collection.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@
11551155
"header": [],
11561156
"body": {
11571157
"mode": "raw",
1158-
"raw": "{\n \"publishingGuidance\": \"I acknowledge that I have reviewed the dashboard and it is ready to publish\",\n \"updatedAt\": \"2020-12-01T20:14:22.190Z\"\n}",
1158+
"raw": "{\n \"publishingGuidance\": \"I acknowledge that I have reviewed the dashboard and it is ready to publish\",\n \"updatedAt\": \"2020-12-01T20:14:22.190Z\"\n \"analyticsTrackingId\": \"UA123\"\n}",
11591159
"options": {
11601160
"raw": {
11611161
"language": "json"
@@ -1181,7 +1181,7 @@
11811181
"header": [],
11821182
"body": {
11831183
"mode": "raw",
1184-
"raw": "{\n \"title\": \"Performance Dashboard\",\n \"description\": \"The Performance Dashboard makes data open and accessible to provide transparency and helps drive the ongoing improvement of digital services.\",\n \"metricsScript\": \"<script></script>\",\n \"updatedAt\": \"2020-12-03T17:22:20.464Z\"\n}",
1184+
"raw": "{\n \"title\": \"Performance Dashboard\",\n \"description\": \"The Performance Dashboard makes data open and accessible to provide transparency and helps drive the ongoing improvement of digital services.\",\n \"updatedAt\": \"2020-12-03T17:22:20.464Z\"\n}",
11851185
"options": {
11861186
"raw": {
11871187
"language": "json"

backend/src/lib/controllers/__tests__/homepage-ctrl.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ describe("getPublicHomepage", () => {
4343
HomepageFactory.getDefaultHomepage = jest.fn().mockReturnValueOnce({
4444
title: "Performance Dashboard",
4545
description: "Welcome to the performance dashboard",
46-
metricsScript: "<script></script>",
4746
});
4847

4948
await HomepageCtrl.getPublicHomepage(req, res);
@@ -52,7 +51,6 @@ describe("getPublicHomepage", () => {
5251
expect.objectContaining({
5352
title: "Performance Dashboard",
5453
description: "Welcome to the performance dashboard",
55-
metricsScript: "<script></script>",
5654
})
5755
);
5856
});
@@ -62,7 +60,6 @@ describe("getPublicHomepage", () => {
6260
repository.getHomepage = jest.fn().mockReturnValueOnce({
6361
title: "Kingdom of Wakanda",
6462
description: "Welcome to the performance dashboard of our kingdom",
65-
metricsScript: "<script></script>",
6663
});
6764

6865
await HomepageCtrl.getPublicHomepage(req, res);
@@ -71,7 +68,6 @@ describe("getPublicHomepage", () => {
7168
expect.objectContaining({
7269
title: "Kingdom of Wakanda",
7370
description: "Welcome to the performance dashboard of our kingdom",
74-
metricsScript: "<script></script>",
7571
})
7672
);
7773
});
@@ -132,7 +128,6 @@ describe("getHomepage", () => {
132128
HomepageFactory.getDefaultHomepage = jest.fn().mockReturnValueOnce({
133129
title: "Performance Dashboard",
134130
description: "Welcome to the performance dashboard",
135-
metricsScript: "<script></script>",
136131
});
137132

138133
await HomepageCtrl.getHomepage(req, res);
@@ -141,7 +136,6 @@ describe("getHomepage", () => {
141136
expect.objectContaining({
142137
title: "Performance Dashboard",
143138
description: "Welcome to the performance dashboard",
144-
metricsScript: "<script></script>",
145139
})
146140
);
147141
});
@@ -151,7 +145,6 @@ describe("getHomepage", () => {
151145
repository.getHomepage = jest.fn().mockReturnValueOnce({
152146
title: "Kingdom of Wakanda",
153147
description: "Welcome to the performance dashboard of our kingdom",
154-
metricsScript: "<script></script>",
155148
});
156149

157150
await HomepageCtrl.getHomepage(req, res);
@@ -160,7 +153,6 @@ describe("getHomepage", () => {
160153
expect.objectContaining({
161154
title: "Kingdom of Wakanda",
162155
description: "Welcome to the performance dashboard of our kingdom",
163-
metricsScript: "<script></script>",
164156
})
165157
);
166158
});
@@ -177,7 +169,6 @@ describe("updateHomepage", () => {
177169
body: {
178170
title: "abc",
179171
description: "description test",
180-
metricsScript: "<script></script>",
181172
updatedAt: now.toISOString(),
182173
},
183174
} as any as Request;
@@ -209,7 +200,6 @@ describe("updateHomepage", () => {
209200
expect(repository.updateHomepage).toHaveBeenCalledWith(
210201
"abc",
211202
"description test",
212-
"<script></script>",
213203
now.toISOString(),
214204
user
215205
);

backend/src/lib/controllers/__tests__/settings-ctrl.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ describe("updateSettings", () => {
186186
user
187187
);
188188
});
189+
190+
it("updates analytics tracking id", async () => {
191+
req.body.analyticsTrackingId = "UA123";
192+
await SettingsCtrl.updateSettings(req, res);
193+
expect(repository.updateSetting).toHaveBeenCalledWith(
194+
"analyticsTrackingId",
195+
"UA123",
196+
now.toISOString(),
197+
user
198+
);
199+
});
189200
});
190201

191202
describe("getPublicSettings", () => {

backend/src/lib/controllers/homepage-ctrl.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ async function getPublicHomepage(req: Request, res: Response) {
2424
return res.json({
2525
title: homepage.title,
2626
description: homepage.description,
27-
metricsScript: homepage.metricsScript,
2827
dashboards: publicDashboards,
2928
});
3029
}
@@ -43,7 +42,7 @@ async function getHomepage(req: Request, res: Response) {
4342

4443
async function updateHomepage(req: Request, res: Response) {
4544
const user = req.user;
46-
const { title, description, metricsScript, updatedAt } = req.body;
45+
const { title, description, updatedAt } = req.body;
4746

4847
if (!title) {
4948
res.status(400).send("Missing required body `title`");
@@ -61,7 +60,7 @@ async function updateHomepage(req: Request, res: Response) {
6160
}
6261

6362
const repo = HomepageRepository.getInstance();
64-
await repo.updateHomepage(title, description, metricsScript, updatedAt, user);
63+
await repo.updateHomepage(title, description, updatedAt, user);
6564

6665
res.send();
6766
}
@@ -157,7 +156,6 @@ async function getPublicHomepageWithQuery(req: Request, res: Response) {
157156
return res.json({
158157
title: homepage.title,
159158
description: homepage.description,
160-
metricsScript: homepage.metricsScript,
161159
dashboards: publicDashboards,
162160
});
163161
}

backend/src/lib/controllers/settings-ctrl.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async function updateSettings(req: Request, res: Response) {
2626
adminContactEmailAddress,
2727
contactEmailAddress,
2828
contactUsContent,
29+
analyticsTrackingId,
2930
} = req.body;
3031

3132
if (!updatedAt) {
@@ -146,6 +147,15 @@ async function updateSettings(req: Request, res: Response) {
146147
);
147148
}
148149

150+
if (analyticsTrackingId) {
151+
updatedAt = await repo.updateSetting(
152+
"analyticsTrackingId",
153+
analyticsTrackingId,
154+
updatedAt,
155+
user
156+
);
157+
}
158+
149159
res.send();
150160
}
151161

backend/src/lib/factories/__tests__/settings-factory.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe("getDefaultSettings", () => {
2121
primary: "#005EA2",
2222
secondary: "#54278f",
2323
},
24+
analyticsTrackingId: undefined,
2425
});
2526
});
2627
});
@@ -48,6 +49,7 @@ describe("fromItem", () => {
4849
contactEmailAddress: "test@aol.com",
4950
adminContactEmailAddress: "admin@aol.com",
5051
contactUsContent: "test content",
52+
analyticsTrackingId: "UA123",
5153
};
5254

5355
const settings = SettingsFactory.fromItem(item);
@@ -73,6 +75,7 @@ describe("fromItem", () => {
7375
contactEmailAddress: "test@aol.com",
7476
adminContactEmailAddress: "admin@aol.com",
7577
contactUsContent: "test content",
78+
analyticsTrackingId: "UA123",
7679
});
7780
});
7881
});
@@ -100,6 +103,7 @@ describe("toPublicSettings", () => {
100103
},
101104
contactEmailAddress: "test@aol.com",
102105
contactUsContent: "test content",
106+
analyticsTrackingId: "UA123",
103107
};
104108

105109
const publicSettings = SettingsFactory.toPublicSettings(settings);
@@ -121,6 +125,7 @@ describe("toPublicSettings", () => {
121125
},
122126
contactEmailAddress: "test@aol.com",
123127
contactUsContent: "test content",
128+
analyticsTrackingId: "UA123",
124129
});
125130
});
126131
});

backend/src/lib/factories/homepage-factory.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ function fromItem(item: HomepageItem): Homepage {
1515
return {
1616
title: item.title,
1717
description: item.description,
18-
metricsScript: item.metricsScript
19-
? item.metricsScript
20-
: "<script></script>",
2118
updatedAt: item.updatedAt ? new Date(item.updatedAt) : new Date(),
2219
};
2320
}

backend/src/lib/factories/settings-factory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function getDefaultSettings(): Settings {
2222
primary: "#005EA2",
2323
secondary: "#54278f",
2424
},
25+
analyticsTrackingId: undefined,
2526
};
2627
}
2728

@@ -52,6 +53,9 @@ function fromItem(item: SettingsItem): Settings {
5253
contactEmailAddress: item.contactEmailAddress,
5354
adminContactEmailAddress: item.adminContactEmailAddress,
5455
contactUsContent: item.contactUsContent,
56+
analyticsTrackingId: item.analyticsTrackingId
57+
? item.analyticsTrackingId
58+
: defaults.analyticsTrackingId,
5559
};
5660
}
5761

@@ -76,6 +80,9 @@ function toPublicSettings(settings: Settings): PublicSettings {
7680
colors: settings.colors ? settings.colors : defaults.colors,
7781
contactEmailAddress: settings.contactEmailAddress,
7882
contactUsContent: settings.contactUsContent,
83+
analyticsTrackingId: settings.analyticsTrackingId
84+
? settings.analyticsTrackingId
85+
: defaults.analyticsTrackingId,
7986
};
8087
}
8188

backend/src/lib/models/homepage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export interface Homepage {
22
title: string;
33
description: string;
4-
metricsScript?: string;
54
updatedAt: Date;
65
}
76

@@ -12,5 +11,4 @@ export interface HomepageItem {
1211
updatedAt?: string;
1312
title: string;
1413
description: string;
15-
metricsScript?: string;
1614
}

backend/src/lib/models/settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Settings {
2020
contactEmailAddress?: string;
2121
adminContactEmailAddress?: string;
2222
contactUsContent?: string;
23+
analyticsTrackingId?: string; // Only Google Analytics is supported at this time.
2324
}
2425

2526
export interface PublicSettings {
@@ -40,6 +41,7 @@ export interface PublicSettings {
4041
};
4142
contactEmailAddress?: string;
4243
contactUsContent?: string;
44+
analyticsTrackingId?: string; // Only Google Analytics is supported at this time.
4345
}
4446

4547
export interface SettingsItem {
@@ -67,4 +69,5 @@ export interface SettingsItem {
6769
contactEmailAddress?: string;
6870
adminContactEmailAddress?: string;
6971
contactUsContent?: string;
72+
analyticsTrackingId?: string; // Only Google Analytics is supported at this time.
7073
}

0 commit comments

Comments
 (0)