Integrated Analytics into the Infrastructure package (#40)

This PR contains the work done to define the `Analytics` type into the _Infrastructure_ package and also, to integrate this type into its `Page` protocol.

Reviewed-on: rock-n-code/loud-amsterdam#40
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
2026-08-04 11:23:25 +02:00
parent fd5aa84924
commit 5c070c2b1a
5 changed files with 220 additions and 5 deletions
@@ -122,6 +122,40 @@ struct PageTests {
))
}
@Test
func `omits the analytics tracker by default`() {
let html = StubPage().render()
#expect(!html.contains("data-website-id"))
}
@Test
func `renders the analytics tracker when provided`() {
let html = StubPage(analytics: .init(
scriptURL: "https://analytics.example.com/script",
websiteID: "0000-website-id",
domains: "example.com"
)).render()
#expect(html.contains(#"<script defer src="https://analytics.example.com/script" data-website-id="0000-website-id" data-domains="example.com" data-exclude-hash="true" data-do-not-track="true" data-performance="true"></script>"#))
}
@Test
func `omits the analytics behavior flags that are disabled`() {
let html = StubPage(analytics: .init(
scriptURL: "https://analytics.example.com/script",
websiteID: "0000-website-id",
domains: "example.com",
excludeHash: true,
doNotTrack: false,
performance: false
)).render()
#expect(html.contains(#"<script defer src="https://analytics.example.com/script" data-website-id="0000-website-id" data-domains="example.com" data-exclude-hash="true"></script>"#))
#expect(!html.contains("data-do-not-track"))
#expect(!html.contains("data-performance"))
}
@Test
func `appends the version token to the asset URLs`() {
let html = StubPage(assetVersion: "0123456789abcdef").render()
@@ -0,0 +1,55 @@
import Testing
@testable import Infrastructure
@Suite(
"Analytics type",
.tags(.type)
)
struct AnalyticsTests {
// MARK: Functional tests
@Test
func `lists the website id and domains with every behavior flag on by default`() {
let analytics = Analytics(
scriptURL: "https://analytics.example.com/script",
websiteID: "id-123",
domains: "example.com"
)
#expect(analytics.attributes.map(\.name) == [
"data-website-id",
"data-domains",
"data-exclude-hash",
"data-do-not-track",
"data-performance",
])
#expect(analytics.attributes.map(\.value) == [
"id-123",
"example.com",
"true",
"true",
"true",
])
}
@Test
func `omits the disabled behavior flags`() {
let analytics = Analytics(
scriptURL: "https://analytics.example.com/script",
websiteID: "id-123",
domains: "example.com",
excludeHash: true,
doNotTrack: false,
performance: false
)
#expect(analytics.attributes.map(\.name) == [
"data-website-id",
"data-domains",
"data-exclude-hash",
])
}
}