diff --git a/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift b/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift index 3d31dbe52..ea417ad68 100644 --- a/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift +++ b/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift @@ -26,9 +26,13 @@ public func normalizeURL(_ dirtyURL: String) -> String { } urlObject.queryItems = urlObject.queryItems?.filter { item in - item.name.starts(with: "utm_") + !item.name.starts(with: "utm_") } + urlObject.queryItems = urlObject.queryItems?.sorted(by: { first, second in + first.name <= second.name + }) + if /* options.removeTrailingSlash */ true { urlObject.path = urlObject.path.replacingRegex(pattern: "/$", replaceWith: "") } diff --git a/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift b/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift index 2fd256f9f..8439564aa 100644 --- a/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift +++ b/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift @@ -9,7 +9,20 @@ final class UtilsTests: XCTestCase { XCTAssertEqual("Hello", "Hello") } + func testNormalizeUrl() { + // trailing slash removed + XCTAssertEqual(normalizeURL("https://omnivore.app/"), "https://omnivore.app") + + // utm_ removed + XCTAssertEqual(normalizeURL("https://omnivore.app/?aa=a&bb=b&utm_track=track&cc=c"), "https://omnivore.app?aa=a&bb=b&cc=c") + + // query params sorted + XCTAssertEqual(normalizeURL("https://omnivore.app/?aa=a&cc=c&bb=b"), "https://omnivore.app?aa=a&bb=b&cc=c") + XCTAssertEqual(normalizeURL("https://omnivore.app/?cc=c&bb=b&aa=a"), "https://omnivore.app?aa=a&bb=b&cc=c") + } + static var allTests = [ - ("testExample", testExample) + ("testExample", testExample), + ("testNormalizeUrl", testNormalizeUrl) ] }