This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Stats Refresh: Insights fetching #88
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
110e8fe
Introduce a new way to handle fetching data for Stats.
jklausa 0bda3a4
Add a protocol extension for `InsightProtocol.queryProperties` to sim…
jklausa 3886f1d
Add string sanitisation back
jklausa b0cb830
Update podspec.
jklausa d06e971
Address code review notes.
jklausa db40daa
Simplify `InsightProtocol` to use stricter types than `AnyObject` and…
jklausa 8c9a2c6
Simplify an expression to a ternary operator
jklausa b34ba51
Merge branch 'develop' into feature/stats-insights-fetching
jklausa 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| public struct StatsAllTimesInsight { | ||
| public let postsCount: Int | ||
| public let viewsCount: Int | ||
| public let bestViewsDay: Date | ||
| public let visitorsCount: Int | ||
| public let bestViewsPerDayCount: Int | ||
| } | ||
|
|
||
|
|
||
| extension StatsAllTimesInsight: InsightProtocol { | ||
|
|
||
| //MARK: - InsightProtocol Conformance | ||
| public init?(jsonDictionary: [String: AnyObject]) { | ||
| guard | ||
| let statsDict = jsonDictionary["stats"] as? [String: AnyObject], | ||
| let postsCount = statsDict["posts"] as? Int, | ||
| let viewsCount = statsDict["views"] as? Int, | ||
| let visitorsCount = statsDict["visitors"] as? Int, | ||
| let bestViewsPerDayCount = statsDict["views_best_day_total"] as? Int, | ||
| let bestViewsDayString = statsDict["views_best_day"] as? String, | ||
| let bestViewsDay = StatsAllTimesInsight.dateFormatter.date(from: bestViewsDayString) | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| self.postsCount = postsCount | ||
| self.bestViewsPerDayCount = bestViewsPerDayCount | ||
| self.visitorsCount = visitorsCount | ||
| self.viewsCount = viewsCount | ||
| self.bestViewsDay = bestViewsDay | ||
| } | ||
|
|
||
| //MARK: - | ||
| private static var dateFormatter: DateFormatter { | ||
| let formatter = DateFormatter() | ||
| formatter.dateFormat = "yyyy-MM-dd" | ||
| return formatter | ||
| } | ||
| } | ||
85 changes: 85 additions & 0 deletions
85
WordPressKit/Insights/StatsAnnualAndMostPopularTimeInsight.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,85 @@ | ||
| public struct StatsAnnualAndMostPopularTimeInsight { | ||
|
|
||
| /// - A `DateComponents` object with one field populated: `weekday`. | ||
| public let mostPopularDayOfWeek: DateComponents | ||
| public let mostPopularDayOfWeekPercentage: Int | ||
|
|
||
| /// - A `DateComponents` object with one field populated: `hour`. | ||
| public let mostPopularHour: DateComponents | ||
| public let mostPopularHourPercentage: Int | ||
|
|
||
| public let annualInsightsYear: Int | ||
|
|
||
| public let annualInsightsTotalPostsCount: Int | ||
| public let annualInsightsTotalWordsCount: Int | ||
| public let annualInsightsAverageWordsCount: Double | ||
|
|
||
| public let annualInsightsTotalLikesCount: Int | ||
| public let annualInsightsAverageLikesCount: Double | ||
|
|
||
| public let annualInsightsTotalCommentsCount: Int | ||
| public let annualInsightsAverageCommentsCount: Double | ||
|
|
||
| public let annualInsightsTotalImagesCount: Int | ||
| public let annualInsightsAverageImagesCount: Double | ||
| } | ||
|
|
||
| extension StatsAnnualAndMostPopularTimeInsight: InsightProtocol { | ||
| public static var pathComponent: String { | ||
| return "stats/insights" | ||
| } | ||
|
|
||
| public init?(jsonDictionary: [String : AnyObject]) { | ||
| guard | ||
| let highestHour = jsonDictionary["highest_hour"] as? Int, | ||
| let highestHourPercentageValue = jsonDictionary["highest_hour_percent"] as? Double, | ||
| let highestDayOfWeek = jsonDictionary["highest_day_of_week"] as? Int, | ||
| let highestDayOfWeekPercentageValue = jsonDictionary["highest_day_percent"] as? Double, | ||
| let yearlyInsights = jsonDictionary["years"] as? [[String: AnyObject]], | ||
| let latestYearlyInsight = yearlyInsights.last, | ||
| let yearString = latestYearlyInsight["year"] as? String, | ||
| let currentYear = Int(yearString), | ||
| let postCount = latestYearlyInsight["total_posts"] as? Int, | ||
| let wordsCount = latestYearlyInsight["total_words"] as? Int, | ||
| let wordsAverage = latestYearlyInsight["avg_words"] as? Double, | ||
| let likesCount = latestYearlyInsight["total_likes"] as? Int, | ||
| let likesAverage = latestYearlyInsight["avg_likes"] as? Double, | ||
| let commentsCount = latestYearlyInsight["total_comments"] as? Int, | ||
| let commentsAverage = latestYearlyInsight["avg_comments"] as? Double, | ||
| let imagesCount = latestYearlyInsight["total_images"] as? Int, | ||
| let imagesAverage = latestYearlyInsight["avg_images"] as? Double | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| let mappedWeekday: ((Int) -> Int) = { | ||
| // iOS Calendar system is `1-based` and uses Sunday as the first day of the week. | ||
| // The data returned from WP.com is `0-based` and uses Monday as the first day of the week. | ||
| // This maps the WP.com data to iOS format. | ||
| return $0 == 6 ? 0 : $0 + 2 | ||
| } | ||
|
|
||
| let weekDayComponent = DateComponents(weekday: mappedWeekday(highestDayOfWeek)) | ||
| let hourComponents = DateComponents(hour: highestHour) | ||
|
|
||
| self.mostPopularDayOfWeek = weekDayComponent | ||
| self.mostPopularDayOfWeekPercentage = Int(highestDayOfWeekPercentageValue) | ||
| self.mostPopularHour = hourComponents | ||
| self.mostPopularHourPercentage = Int(highestHourPercentageValue) | ||
|
|
||
| self.annualInsightsYear = currentYear | ||
|
|
||
| self.annualInsightsTotalPostsCount = postCount | ||
| self.annualInsightsTotalWordsCount = wordsCount | ||
| self.annualInsightsAverageWordsCount = wordsAverage | ||
|
|
||
| self.annualInsightsTotalLikesCount = likesCount | ||
| self.annualInsightsAverageLikesCount = likesAverage | ||
|
|
||
| self.annualInsightsTotalCommentsCount = commentsCount | ||
| self.annualInsightsAverageCommentsCount = commentsAverage | ||
|
|
||
| self.annualInsightsTotalImagesCount = imagesCount | ||
| self.annualInsightsAverageImagesCount = imagesAverage | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.