Skip to content
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

Add endpoint for fetching subscriber history #795

Merged
merged 15 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _None._
- Add `getPost(withID)` to `PostServiceRemoteExtended` [#785]
- Add support for metadata to `PostServiceRemoteExtended` [#783]
- Add fetching of `StatsEmailsSummaryData` to `StatsService` [#794]
- Add fetching of `StatsSubscribersSummaryData` to `StatsService` [#795]

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Foundation
import WordPressShared

public struct StatsSubscribersSummaryData: Decodable, Equatable {
public let history: [SubscriberData]

public init(history: [SubscriberData]) {
self.history = history
}
}

extension StatsSubscribersSummaryData {
public static var pathComponent: String {
return "stats/subscribers"
}

static var dateFormatter: DateFormatter = {
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POS")
df.dateFormat = "yyyy-MM-dd"
return df
}()

public struct SubscriberData: Decodable, Equatable {
public let date: Date
public let count: Int

public init(date: Date, count: Int) {
self.date = date
self.count = count
}
}

public init?(jsonDictionary: [String: AnyObject]) {
guard
let fields = jsonDictionary["fields"] as? [String],
let data = jsonDictionary["data"] as? [[Any]],
let dateIndex = fields.firstIndex(of: "period"),
let countIndex = fields.firstIndex(of: "subscribers")
guarani marked this conversation as resolved.
Show resolved Hide resolved
else {
return nil
}

let history: [SubscriberData?] = data.map { elements in
guard elements.indices.contains(dateIndex) && elements.indices.contains(countIndex),
let dateString = elements[dateIndex] as? String,
let date = StatsSubscribersSummaryData.dateFormatter.date(from: dateString),
let count = elements[countIndex] as? Int
else {
return nil
}

return SubscriberData(date: date, count: count)
}

let sorted = history.compactMap { $0 }.sorted(by: { $0.date.compare($1.date) == .orderedAscending })
guarani marked this conversation as resolved.
Show resolved Hide resolved
self = .init(history: sorted)
}

public static func queryProperties(quantity: Int, unit: StatsSubscribersSummaryData.Unit) -> [String: String] {
return ["quantity": String(quantity), "unit": unit.rawValue]
}

public enum Unit: String {
case day = "day"
}
}
24 changes: 24 additions & 0 deletions Sources/WordPressKit/Services/StatsServiceRemoteV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,30 @@ private extension StatsServiceRemoteV2 {
}
}

// MARK: - Subscribers Data

public extension StatsServiceRemoteV2 {
guarani marked this conversation as resolved.
Show resolved Hide resolved
func getSubscribers(unit: StatsSubscribersSummaryData.Unit,
completion: @escaping ((Result<StatsSubscribersSummaryData, Error>) -> Void)) {
let pathComponent = StatsSubscribersSummaryData.pathComponent
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
let properties = StatsSubscribersSummaryData.queryProperties(quantity: 30, unit: unit) as [String: AnyObject]

wordPressComRESTAPI.get(path, parameters: properties, success: { (response, _) in
guard let jsonResponse = response as? [String: AnyObject],
let subscribersSummaryData = StatsSubscribersSummaryData(jsonDictionary: jsonResponse)
else {
completion(.failure(ResponseError.decodingFailure))
return
}

completion(.success(subscribersSummaryData))
}, failure: { (error, _) in
completion(.failure(error))
})
}
}

// MARK: - Emails Summary

public extension StatsServiceRemoteV2 {
Expand Down
Loading