Skip to content

Commit

Permalink
Add max_x_offset and content_width to screen_summary entity
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Dec 12, 2023
1 parent cb4a970 commit 0fb21c6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
10 changes: 8 additions & 2 deletions Sources/Core/ScreenViewTracking/ScreenSummaryState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand All @@ -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
}

Expand Down Expand Up @@ -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) }
}

}
50 changes: 42 additions & 8 deletions Sources/Snowplow/Events/ScrollChanged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,62 @@ 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 {
return kSPScrollChangedSchema
}

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
}
}
4 changes: 3 additions & 1 deletion Tests/ScreenViewTracking/TestScreenSummaryStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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"))

Expand Down

0 comments on commit 0fb21c6

Please sign in to comment.