-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add detailed Reader screen tracking #25280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kean
wants to merge
1
commit into
trunk
Choose a base branch
from
task/reader-tracking
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
WordPress/Classes/Utility/Analytics/ScreenTracking+IDs.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import Foundation | ||
|
|
||
| enum ScreenID { | ||
| /// Reader screen identifiers. | ||
| enum Reader { | ||
| static let sidebar = "reader.sidebar" | ||
| static let discover = "reader.discover" | ||
| static let following = "reader.following" | ||
| static let saved = "reader.saved" | ||
| static let likes = "reader.likes" | ||
| static let tag = "reader.tag" | ||
| static let site = "reader.site" | ||
| static let list = "reader.list" | ||
| static let organization = "reader.organization" | ||
| static let search = "reader.search" | ||
| static let article = "reader.article" | ||
| static let comments = "reader.comments" | ||
| static let subscriptions = "reader.subscriptions" | ||
| static let selectInterests = "reader.select_interests" | ||
| } | ||
| } | ||
|
|
||
| enum ElementID { | ||
| /// Reader trigger component identifiers. | ||
| enum Reader { | ||
| static let postCard = "post_card" | ||
| static let postCardComment = "post_card.comment" | ||
| static let postHeaderSiteName = "post_header.site_name" | ||
| static let suggestedSitesCard = "suggested_sites_card" | ||
| static let suggestedTagsCard = "suggested_tags_card" | ||
| static let relatedPosts = "related_posts" | ||
| static let articleHeaderSiteName = "article_header.site_name" | ||
| static let tagChip = "tag_chip" | ||
| static let commentsSection = "comments_section" | ||
| static let toolbarComment = "toolbar.comment" | ||
| static let searchResult = "search_result" | ||
| static let subscriptionCell = "subscription_cell" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import Foundation | ||
| import WordPressShared | ||
|
|
||
| extension WPAnalytics { | ||
| /// Fires the `screen_shown` event. | ||
| static func track( | ||
| screen: String, | ||
| context: ScreenTrackingContext = ScreenTrackingContext(), | ||
| properties: [String: String] = [:] | ||
| ) { | ||
| var props = context.properties | ||
| props["screen"] = screen | ||
| props.merge(properties) { _, new in new } | ||
| WPAnalytics.track(.screenShown, properties: props) | ||
| } | ||
| } | ||
|
|
||
| /// Describes the source screen and trigger that led to a navigation. | ||
| struct ScreenTrackingSource { | ||
| /// The screen the user navigated from (e.g. `"reader.discover"`). | ||
| let screen: String | ||
|
|
||
| /// A sub-section within the screen (e.g. `"recommended"` for a Discover tab). | ||
| var section: String? | ||
|
|
||
| /// The UI element that triggered the navigation (e.g. `"post_card"`). | ||
| var component: String? | ||
|
|
||
| /// Position index for list items (0-based). | ||
| var position: Int? | ||
|
|
||
| init(_ screen: String, section: String? = nil, component: String? = nil, position: Int? = nil) { | ||
| self.screen = screen | ||
| self.section = section | ||
| self.component = component | ||
| self.position = position | ||
| } | ||
| } | ||
|
|
||
| /// Describes how the user arrived at the current screen. | ||
| struct ScreenTrackingContext { | ||
| var source: ScreenTrackingSource? | ||
|
|
||
| /// Builds the properties dictionary for the analytics event. | ||
| var properties: [String: String] { | ||
| var props: [String: String] = [:] | ||
| if let source { | ||
| props["source"] = [source.screen, source.section, source.component] | ||
| .compactMap { $0 } | ||
| .joined(separator: ".") | ||
| props["source_screen"] = source.screen | ||
| if let section = source.section { | ||
| props["source_section"] = section | ||
| } | ||
| if let component = source.component { | ||
| props["source_component"] = component | ||
| if let position = source.position { | ||
| props["source_component_position"] = String(position) | ||
| } | ||
| } | ||
| } | ||
| return props | ||
| } | ||
| } | ||
| // MARK: - UIViewController | ||
|
|
||
| import UIKit | ||
|
|
||
| extension UIViewController { | ||
| /// Tracking context describing how the user arrived at this screen. | ||
| var trackingContext: ScreenTrackingContext { | ||
| get { objc_getAssociatedObject(self, &AssociatedKeys.trackingContext) as? ScreenTrackingContext ?? ScreenTrackingContext() } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should store the |
||
| set { objc_setAssociatedObject(self, &AssociatedKeys.trackingContext, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } | ||
| } | ||
| } | ||
|
|
||
| private enum AssociatedKeys { | ||
| static var trackingContext: UInt8 = 0 | ||
| } | ||
|
|
||
| // MARK: - SwiftUI | ||
|
|
||
| import SwiftUI | ||
|
|
||
| private struct ScreenTrackingContextKey: EnvironmentKey { | ||
| static let defaultValue = ScreenTrackingContext() | ||
| } | ||
|
|
||
| extension EnvironmentValues { | ||
| var trackingContext: ScreenTrackingContext { | ||
| get { self[ScreenTrackingContextKey.self] } | ||
| set { self[ScreenTrackingContextKey.self] = newValue } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it for simplify. It was used for the "Subscriptions" screen on iPad. I initially enable large title there, but it looks great without it β and consistent with other screens.