Turned the analytics tracker off by default in the template.

This commit is contained in:
2026-08-13 02:57:47 +02:00
parent b68eac4375
commit 2206d71997
13 changed files with 178 additions and 71 deletions
@@ -12,36 +12,47 @@ struct ConfigReaderPropertiesTests {
// MARK: Functional tests
@Test
func `analytics to default to the production tracker`() throws {
let analytics = try #require(reader().analytics)
#expect(analytics.scriptURL == .Analytics.scriptURL)
#expect(analytics.websiteID == .Analytics.websiteID)
#expect(analytics.domains == .Analytics.domains)
#expect(analytics.excludeHash)
#expect(analytics.doNotTrack)
#expect(analytics.performance)
#expect(analytics.recorder)
func `analytics to be omitted by default`() {
// The template ships no website identifier, so an unconfigured deployment embeds no tracker at all.
#expect(.Analytics.websiteID == "")
#expect(reader().analytics == nil)
}
@Test
func `analytics to switch recorder mode off when configured`() throws {
func `analytics to be enabled by setting the website id alone`() throws {
let analytics = try #require(reader(values: [
.Analytics.recorder: false
.Analytics.websiteID: "0000-website-id"
]).analytics)
#expect(analytics.scriptURL == .Analytics.scriptURL)
#expect(analytics.websiteID == "0000-website-id")
#expect(analytics.domains.isEmpty)
#expect(analytics.excludeHash)
#expect(analytics.doNotTrack)
#expect(analytics.performance)
#expect(!analytics.recorder)
}
@Test
func `analytics to switch recorder mode on when configured`() throws {
// Session recording is the most invasive thing the tracker does, so it is opted into rather than out of.
let analytics = try #require(reader(values: [
.Analytics.websiteID: "0000-website-id",
.Analytics.recorder: true
]).analytics)
#expect(analytics.recorder)
}
@Test
func `analytics to override the website id and domains when configured`() throws {
let analytics = try #require(reader(values: [
.Analytics.websiteID: "custom-website-id",
.Analytics.domains: "staging.loud.amsterdam"
.Analytics.domains: "staging.example.com"
]).analytics)
#expect(analytics.websiteID == "custom-website-id")
#expect(analytics.domains == "staging.loud.amsterdam")
#expect(analytics.domains == "staging.example.com")
#expect(analytics.scriptURL == .Analytics.scriptURL)
}
@@ -131,6 +131,42 @@ struct RootControllerTests {
}
}
@Test
func `embeds no analytics tracker by default`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(!body.contains("data-website-id"))
#expect(!body.contains("analytics"))
}
}
}
@Test
func `embeds the analytics tracker when one is configured`() async throws {
try await app(
analytics: .init(
scriptURL: "https://analytics.example.com/script",
websiteID: "0000-website-id",
domains: "example.com"
)
).test(.router) { client in
try await client.execute(
uri: "/",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(body.contains(#"<link rel="preconnect" href="https://analytics.example.com">"#))
#expect(body.contains(#"<script defer src="https://analytics.example.com/script" data-website-id="0000-website-id" data-domains="example.com""#))
}
}
}
}
// MARK: - Helpers
@@ -140,11 +176,14 @@ private extension RootControllerTests {
// MARK: Methods
/// Builds an application whose root controller appends the given version token to the landing
/// page's asset URLs.
/// - Parameter assetVersion: the version token appended to the page's asset URLs.
/// page's asset URLs and embeds the given analytics tracker.
/// - Parameters:
/// - assetVersion: the version token appended to the page's asset URLs.
/// - analytics: the analytics tracker the landing page embeds, or `nil` (the default) to omit it.
/// - Returns: the configured application.
func app(
assetVersion: String?
assetVersion: String? = nil,
analytics: Analytics? = nil
) -> some ApplicationProtocol {
let router = Router(context: WebsiteRequestContext.self)
@@ -153,7 +192,8 @@ private extension RootControllerTests {
}
router.addRoutes(RootController<WebsiteRequestContext>(
assetVersion: assetVersion
assetVersion: assetVersion,
analytics: analytics
).routes)
return Application(router: router)