diff --git a/Sources/Core/ScreenViewTracking/ScreenSummaryState.swift b/Sources/Core/ScreenViewTracking/ScreenSummaryState.swift index 6d3766040..ea57fc85b 100644 --- a/Sources/Core/ScreenViewTracking/ScreenSummaryState.swift +++ b/Sources/Core/ScreenViewTracking/ScreenSummaryState.swift @@ -23,7 +23,9 @@ class ScreenSummaryState: State { var lastItemIndex: Int? var itemsCount: Int? var maxYOffset: Int? + var maxXOffset: Int? var contentHeight: Int? + var contentWidth: Int? var data: [String: Any] { var data: [String: Any] = [ @@ -33,7 +35,9 @@ class ScreenSummaryState: State { if let lastItemIndex = lastItemIndex { data["last_item_index"] = lastItemIndex } if let itemsCount = itemsCount { data["items_count"] = itemsCount } if let maxYOffset = maxYOffset { data["max_y_offset"] = maxYOffset } + if let maxXOffset = maxXOffset { data["max_x_offset"] = maxXOffset } if let contentHeight = contentHeight { data["content_height"] = contentHeight } + if let contentWidth = contentWidth { data["content_width"] = contentWidth } return data } @@ -66,8 +70,10 @@ class ScreenSummaryState: State { } func updateWithScrollChanged(_ event: ScrollChanged) { - maxYOffset = max(event.yOffset, maxYOffset ?? 0) - contentHeight = max(event.contentHeight, contentHeight ?? 0) + if let yOffset = event.yOffset { maxYOffset = max(yOffset, maxYOffset ?? 0) } + if let xOffset = event.xOffset { maxXOffset = max(xOffset, maxXOffset ?? 0) } + if let height = event.contentHeight { contentHeight = max(height, contentHeight ?? 0) } + if let width = event.contentWidth { contentWidth = max(width, contentWidth ?? 0) } } } diff --git a/Sources/Snowplow/Events/ScrollChanged.swift b/Sources/Snowplow/Events/ScrollChanged.swift index 44a62bc63..98f5b6b20 100644 --- a/Sources/Snowplow/Events/ScrollChanged.swift +++ b/Sources/Snowplow/Events/ScrollChanged.swift @@ -20,18 +20,50 @@ import Foundation @objc(SPScrollChanged) public class ScrollChanged: SelfDescribingAbstract { /// Vertical scroll offset in pixels - @objc - public var yOffset: Int + public var yOffset: Int? + /// Horizontal scroll offset in pixels + public var xOffset: Int? /// The height of the scroll view content in pixels - public var contentHeight: Int + public var contentHeight: Int? + /// The width of the scroll view content in pixels + public var contentWidth: Int? /// - Parameters: /// - yOffset: Vertical scroll offset in pixels /// - contentHeight: The height of the scroll view content in pixels + public init(xOffset: Int? = nil, yOffset: Int? = nil, contentWidth: Int? = nil, contentHeight: Int? = nil) { + self.yOffset = yOffset + self.xOffset = xOffset + self.contentHeight = contentHeight + self.contentWidth = contentWidth + } + + /// Vertical scroll offset in pixels @objc - public init(yOffset: Int, contentHeight: Int) { + public func yOffset(_ yOffset: Int) -> Self { self.yOffset = yOffset + return self + } + + /// Horizontal scroll offset in pixels + @objc + public func xOffset(_ xOffset: Int) -> Self { + self.xOffset = xOffset + return self + } + + /// The height of the scroll view content in pixels + @objc + public func contentHeight(_ contentHeight: Int) -> Self { self.contentHeight = contentHeight + return self + } + + /// The width of the scroll view content in pixels + @objc + public func contentWidth(_ contentWidth: Int) -> Self { + self.contentWidth = contentWidth + return self } override var schema: String { @@ -39,9 +71,11 @@ public class ScrollChanged: SelfDescribingAbstract { } override var payload: [String : Any] { - return [ - "y_offset": yOffset, - "content_height": contentHeight - ] + var data: [String: Any] = [:] + if let xOffset = xOffset { data["x_offset"] = xOffset } + if let yOffset = yOffset { data["y_offset"] = yOffset } + if let contentWidth = contentWidth { data["content_width"] = contentWidth } + if let contentHeight = contentHeight { data["content_height"] = contentHeight } + return data } } diff --git a/Tests/ScreenViewTracking/TestScreenSummaryStateMachine.swift b/Tests/ScreenViewTracking/TestScreenSummaryStateMachine.swift index bf8b41eb6..411481996 100644 --- a/Tests/ScreenViewTracking/TestScreenSummaryStateMachine.swift +++ b/Tests/ScreenViewTracking/TestScreenSummaryStateMachine.swift @@ -109,7 +109,9 @@ class TestScreenSummaryStateMachine: XCTestCase { if event.schema == kSPScreenEndSchema { let entity = event.entities.first { $0.schema == kSPScreenSummarySchema } XCTAssertEqual((entity?.data as? [String: Any])?["max_y_offset"] as? Int, 30) + XCTAssertEqual((entity?.data as? [String: Any])?["max_x_offset"] as? Int, 15) XCTAssertEqual((entity?.data as? [String: Any])?["content_height"] as? Int, 100) + XCTAssertEqual((entity?.data as? [String: Any])?["content_width"] as? Int, 200) expectScreenEnd.fulfill() } } @@ -118,7 +120,7 @@ class TestScreenSummaryStateMachine: XCTestCase { _ = tracker.track(ScreenView(name: "Screen 1")) _ = tracker.track(ScrollChanged(yOffset: 10, contentHeight: 100)) - _ = tracker.track(ScrollChanged(yOffset: 30, contentHeight: 100)) + _ = tracker.track(ScrollChanged(xOffset: 15, yOffset: 30, contentWidth: 200, contentHeight: 100)) _ = tracker.track(ScrollChanged(yOffset: 20, contentHeight: 100)) _ = tracker.track(ScreenView(name: "Screen 2"))