Skip to content

Rewrite AnalyticsTrackerAutomatticTracks in Swift - #24399

Merged
kean merged 2 commits into
trunkfrom
task/rewrite-automattic-tracks
Apr 8, 2025
Merged

Rewrite AnalyticsTrackerAutomatticTracks in Swift#24399
kean merged 2 commits into
trunkfrom
task/rewrite-automattic-tracks

Conversation

@kean

@kean kean commented Apr 2, 2025

Copy link
Copy Markdown
Contributor

This class had a lot of Swift APIs that it depended on. Prerequisite for Keystone. I carefully translated the code and compared it side-by-side multiple times. The risk of introducing user-facing regression is none because it's tracking.

To test:

  • Add a self-hosted site and verify that session starts as anonymous (step with a debugger)
  • Add a WP.com account and verify that session starts with the username/token

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

  3. What automated tests I added (or what prevented me from doing so)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@kean kean added the Analytics label Apr 2, 2025
@kean kean added this to the 25.9 milestone Apr 2, 2025
@dangermattic

Copy link
Copy Markdown
Collaborator
2 Warnings
⚠️ This PR is larger than 500 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
⚠️ This PR is assigned to the milestone 25.9. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger

@kean
kean requested review from crazytonyli and mokagio and removed request for crazytonyli April 2, 2025 23:21
let name: String
var properties: [String: Any]?

switch stat {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no AI used – only search&replace, so there is no need to verify it.

@wpmobilebot

wpmobilebot commented Apr 2, 2025

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number27202
VersionPR #24399
Bundle IDcom.jetpack.alpha
Commit84ffe73
Installation URL553blrmomqeo8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Apr 2, 2025

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number27202
VersionPR #24399
Bundle IDorg.wordpress.alpha
Commit84ffe73
Installation URL6l24ajbfe7uh0
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

var mergedProperties: [AnyHashable: Any] = event.properties ?? [:]
for (key, value) in properties ?? [:] {
mergedProperties[key] = value
}

@crazytonyli crazytonyli Apr 3, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: The above could be implemented in one line.

let mergedProperties = (event.properties ?? [:]).merging(properties ?? [:]) { (_, new) in new }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I'm not sure why I keep avoiding merging. It is more clear and less code this way.

track(stat, withProperties: nil)
}

public func track(_ stat: WPAnalyticsStat, withProperties properties: [AnyHashable: Any]?) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the key of properties be String?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's how it's defined in the protocol. I would ideally update it to [String: EnumWithSupportedTypesOnly] to avoid any confusion.

return
}

if currentUserID?.isEmpty == true {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition should be currentUserID?.isEmpty != false because currentUserID = nil should execute the if branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous condition was the following:

if (self.loggedInID.length == 0) {

The logic is that if you don't have any currentUserID saved, then call switchToAuthenticatedUser with skipAliasEventCreation: false. A != nil check should probably be enough, but I used isEmpty just for to be safe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I added the comment because the condition check has changed. The previous condition is true if loggedInID is nil, but the new condition is false if currentUserID is nil.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition should be currentUserID?.isEmpty != false

It's the same as currentUserID?.isEmpty == true. It's true if currentUserID is empty.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they are the same. currentUserID?.isEmpty has three possible values: Optional.some(true), .some(false), and Optional.none. If currentUserID is nil, currentUserID?.isEmpty would be nil and the if condition is false, which is the opssite of the original Objective-C code if (self.loggedInID.length == 0).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah you are totally right. I don't know who I missed that 🤦

I didn't want to move the conditions around, so I replaced this condition with the following (not great, but should be clear enough):

if (currentUserID ?? "").isEmpty

In the future, I feel like I'll just stop relying on these optional tricks.

@kean
kean requested a review from crazytonyli April 3, 2025 12:13
@kean
kean force-pushed the task/rewrite-automattic-tracks branch from 479119b to 4181f81 Compare April 3, 2025 13:37
@kean kean mentioned this pull request Apr 3, 2025
14 tasks
@kean
kean force-pushed the task/rewrite-automattic-tracks branch from ba6c0ef to 4d2b2ff Compare April 7, 2025 12:28
@kean

kean commented Apr 8, 2025

Copy link
Copy Markdown
Contributor Author

Hey, let's get this one merged folks, cc @mokagio , @crazytonyli

@kean
kean added this pull request to the merge queue Apr 8, 2025
Merged via the queue into trunk with commit b246225 Apr 8, 2025
@kean
kean deleted the task/rewrite-automattic-tracks branch April 8, 2025 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants