Skip to content

Commit 4f35758

Browse files
authored
Update readme (#1)
1 parent e1f286e commit 4f35758

File tree

7 files changed

+124
-35
lines changed

7 files changed

+124
-35
lines changed

Package.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
.library(
1313
name: "Analytics",
1414
targets: ["Analytics"]
15-
),
15+
)
1616
],
1717
dependencies: [
1818
.package(url: "https://github.com/apple/swift-http-types", from: "1.3.0"),
@@ -35,9 +35,6 @@ let package = Package(
3535
dependencies: [
3636
.target(name: "Analytics"),
3737
.product(name: "HTTPTypesFoundation", package: "swift-http-types"),
38-
],
39-
resources: [
40-
.process("Resources")
4138
]
4239
),
4340
]

README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,93 @@
11
# firebaseanalytics-swift
22

3-
Firebase Analytics client for Swift.
3+
> [!IMPORTANT]
4+
> This project is under development
5+
6+
Firebase Analytics Client for Swift.
7+
8+
```swift
9+
let clientInformation: ClientInformation = .init(
10+
os: "ios",
11+
osVersion: "18.3",
12+
deviceModel: "iPhone17,3",
13+
locale: "ja-jp",
14+
localeId: 540,
15+
installMethod: "manual_install",
16+
bundleId: "com.apple.SampleApp",
17+
appVersion: "1.0",
18+
googleAppId: "1:21332412389:ios:11681asd2a3cd5d8"
19+
)
20+
21+
let session: Session = .init(
22+
firstInteraction: false,
23+
firstOpenTime: 1_737_550_800_666,
24+
nonPersonalizedAds: false,
25+
sessionId: 1_737_555_666,
26+
sessionNumber: 2,
27+
lifetimeEngagement: 1_175_666,
28+
sessionEngagement: 10055,
29+
userId: "user-id"
30+
)
31+
32+
let payload = Payload(
33+
value: 1,
34+
events: [
35+
EventPayload(
36+
eventName: "login",
37+
parameters: [
38+
.method: "Google",
39+
.platform: "app",
40+
],
41+
previousTimestampMillis: UInt(Date.now.timeIntervalSince1970 * 100),
42+
timestampMillis: UInt(Date.now.timeIntervalSince1970 * 100)
43+
),
44+
EventPayload(
45+
eventName: "_e",
46+
parameters: [
47+
"_et": .uint(891),
48+
.platform: "auto"
49+
],
50+
previousTimestampMillis: UInt(Date.now.timeIntervalSince1970 * 100),
51+
timestampMillis: UInt(Date.now.timeIntervalSince1970 * 100)
52+
),
53+
],
54+
sessionInformations: session.parameters,
55+
value4: UInt(Date.now.timeIntervalSince1970 * 100), // upload_timestamp_millis
56+
value5: UInt(Date.now.timeIntervalSince1970 * 100), // start_timestamp_millis
57+
value6: UInt(Date.now.timeIntervalSince1970 * 100), // end_timestamp_millis
58+
value7: UInt(Date.now.timeIntervalSince1970 * 100), // previous_bundle_end_timestamp_millis
59+
os: clientInformation.os,
60+
osVersion: clientInformation.osVersion,
61+
deviceModel: clientInformation.deviceModel,
62+
value11: clientInformation.locale,
63+
value12: clientInformation.localeId,
64+
installMethod: clientInformation.installMethod,
65+
bundleId: clientInformation.bundleId,
66+
appVersion: clientInformation.appVersion,
67+
value17: 110700, // gmp_version
68+
value18: 110700, // uploading_gmp_version
69+
value21: "BC75E87831231231B7A179248F24080C", // fix // app_instance_id
70+
value23: 25, // count up // bundle_sequential_index
71+
googleAppId: clientInformation.googleAppId,
72+
value26: UInt(Date.now.timeIntervalSince1970 * 100), // free // previous_bundle_start_timestamp_millis
73+
value27: "CAIIJF38-E407-4954-BC16-F693A22F9FA9", // fix // resettable_device_id
74+
value30: "d4Xw8qsfsadBoMYlsYHnot", // fix // firebase_instance_id
75+
value31: 1, // app_version_major
76+
value35: 1_727_127_969_769_945, // fix // config_version
77+
value45: 42_820_019, // fix
78+
value52: "G1--", // fix // consent_signals
79+
value64: "google_signals", // fix
80+
value71: "19911", // fix // consent_diagnostics
81+
value72: 0, // fix
82+
value77: 13 // count up
83+
)
84+
85+
let analytics = Analytics(
86+
httpClient: .urlSession(.shared),
87+
userAgent: "SampleApp/1 CFNetwork/3826.400.120 Darwin/24.3.0",
88+
clientInformation: clientInformation,
89+
session: session
90+
)
91+
92+
try await analytics.log(payload: payload)
93+
```

Sources/Analytics/Analytics.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct Analytics<HTTPClient: HTTPClientProtocol> {
2121
self.clientInformation = clientInformation
2222
self.session = session
2323
}
24-
24+
2525
public func log(payload: Payload) async throws {
2626
let payloadData = try ProtobufEncoder.encoding(NestedValue(value: payload))
2727
try await log(bodyData: payloadData)
@@ -60,23 +60,23 @@ public struct Analytics<HTTPClient: HTTPClientProtocol> {
6060
value: 1,
6161
events: [
6262
EventPayload(
63-
parameters: parameters.merging([.platform: "app"]) { $1 }.compactMapValues { $0 },
6463
eventName: eventName,
64+
parameters: parameters.merging([.platform: "app"]) { $1 }.compactMapValues { $0 },
6565
previousTimestampMillis: UInt(Date.now.timeIntervalSince1970 * 100),
6666
timestampMillis: UInt(Date.now.timeIntervalSince1970 * 100)
6767
),
6868
EventPayload(
69-
parameters: ["_et": .uint(891), .platform: "auto"],
7069
eventName: "_e",
70+
parameters: ["_et": .uint(891), .platform: "auto"],
7171
previousTimestampMillis: UInt(Date.now.timeIntervalSince1970 * 100),
7272
timestampMillis: UInt(Date.now.timeIntervalSince1970 * 100)
7373
),
7474
],
7575
sessionInformations: session.parameters,
76-
value4: UInt(Date.now.timeIntervalSince1970 * 100), // upload_timestamp_millis
77-
value5: UInt(Date.now.timeIntervalSince1970 * 100), // start_timestamp_millis
78-
value6: UInt(Date.now.timeIntervalSince1970 * 100), // end_timestamp_millis
79-
value7: UInt(Date.now.timeIntervalSince1970 * 100), // previous_bundle_end_timestamp_millis
76+
value4: UInt(Date.now.timeIntervalSince1970 * 100), // upload_timestamp_millis
77+
value5: UInt(Date.now.timeIntervalSince1970 * 100), // start_timestamp_millis
78+
value6: UInt(Date.now.timeIntervalSince1970 * 100), // end_timestamp_millis
79+
value7: UInt(Date.now.timeIntervalSince1970 * 100), // previous_bundle_end_timestamp_millis
8080
os: clientInformation.os,
8181
osVersion: clientInformation.osVersion,
8282
deviceModel: clientInformation.deviceModel,
@@ -85,15 +85,16 @@ public struct Analytics<HTTPClient: HTTPClientProtocol> {
8585
installMethod: clientInformation.installMethod,
8686
bundleId: clientInformation.bundleId,
8787
appVersion: clientInformation.appVersion,
88-
value17: 110700, // gmp_version
89-
value18: 110700, // uploading_gmp_version
88+
value17: 110700, // gmp_version
89+
value18: 110700, // uploading_gmp_version
9090
value21: "BC75E8783E8141ECB7A179248F24080C", // fix // app_instance_id
9191
value23: 25, // count up // bundle_sequential_index
9292
googleAppId: clientInformation.googleAppId,
93-
value26: UInt(Date.now.timeIntervalSince1970 * 100), // free // previous_bundle_start_timestamp_millis
93+
// free // previous_bundle_start_timestamp_millis
94+
value26: UInt(Date.now.timeIntervalSince1970 * 100),
9495
value27: "C86A6B98-E407-4954-BC16-F693A22F9FA9", // fix // resettable_device_id
9596
value30: "d4Xw8qsiRUIBoMYlsYHnot", // fix // firebase_instance_id
96-
value31: 1, // app_version_major
97+
value31: 1, // app_version_major
9798
value35: 1_727_127_969_769_945, // fix // config_version
9899
value45: 42_820_019, // fix
99100
value52: "G1--", // fix // consent_signals

Sources/Analytics/Parameter.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum Parameter {
1919
static func bool(_ value: Bool) -> Self {
2020
.uint(value ? 1 : 0)
2121
}
22-
22+
2323
public init(from decoder: inout ProtobufDecoder) throws {
2424
var value: Parameter.Value?
2525

@@ -58,17 +58,17 @@ public enum Parameter {
5858

5959
public func encode(to encoder: inout ProtobufEncoder) throws {
6060
switch self {
61-
case let .string(value):
61+
case .string(let value):
6262
try encoder.stringField(1, value, defaultValue: nil)
63-
case let .uint(value):
63+
case .uint(let value):
6464
encoder.uintField(2, value, defaultValue: nil)
65-
case let .float(value):
65+
case .float(let value):
6666
encoder.doubleField(5, value, defaultValue: nil)
67-
case let .dictionary(value):
67+
case .dictionary(let value):
6868
for (key, value) in value {
6969
try encoder.messageField(6, KeyValue(key: key, value: value))
7070
}
71-
case let .array(values):
71+
case .array(let values):
7272
for item in values {
7373
try encoder.messageField(6, item)
7474
}

Sources/Analytics/Payload/EventPayload.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import ProtobufKit
22

33
public struct EventPayload: ProtobufMessage, Equatable {
4-
public var parameters: [Parameter.Key: Parameter.Value]
54
public var eventName: Event.Name
5+
public var parameters: [Parameter.Key: Parameter.Value]
66
public var previousTimestampMillis: UInt?
77
public var timestampMillis: UInt
88

99
public init(
10-
parameters: [Parameter.Key: Parameter.Value],
1110
eventName: Event.Name,
11+
parameters: [Parameter.Key: Parameter.Value],
1212
previousTimestampMillis: UInt?,
1313
timestampMillis: UInt
1414
) {
@@ -46,8 +46,8 @@ public struct EventPayload: ProtobufMessage, Equatable {
4646

4747
if let eventName, let timestampMillis {
4848
self.init(
49-
parameters: parameters,
5049
eventName: eventName,
50+
parameters: parameters,
5151
previousTimestampMillis: previousTimestampMillis,
5252
timestampMillis: timestampMillis
5353
)
@@ -99,16 +99,16 @@ struct KeyValue: ProtobufMessage {
9999
func encode(to encoder: inout ProtobufEncoder) throws {
100100
try encoder.stringField(1, key.rawValue, defaultValue: nil)
101101
switch value {
102-
case let .string(value):
102+
case .string(let value):
103103
try encoder.stringField(2, value, defaultValue: nil)
104-
case let .uint(value):
104+
case .uint(let value):
105105
encoder.uintField(3, value, defaultValue: nil)
106-
case let .float(value):
106+
case .float(let value):
107107
encoder.doubleField(5, value, defaultValue: nil)
108-
case let .dictionary(values):
108+
case .dictionary(let values):
109109
let arayValue = Array6(value: .dictionary(values))
110110
try encoder.messageField(6, arayValue)
111-
case let .array(values):
111+
case .array(let values):
112112
for value in values {
113113
let arayValue = Array6(value: value)
114114
try encoder.messageField(6, arayValue)

Sources/Analytics/Payload/ValueKeyValue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ public struct ValueKeyValue: ProtobufMessage, Equatable {
6161
encoder.uintField(1, date, defaultValue: nil)
6262
try encoder.stringField(2, key, defaultValue: nil)
6363
switch value {
64-
case let .string(value):
64+
case .string(let value):
6565
try encoder.stringField(3, value, defaultValue: nil)
66-
case let .uint(value):
66+
case .uint(let value):
6767
encoder.uintField(4, value, defaultValue: nil)
68-
case let .float(value):
68+
case .float(let value):
6969
encoder.doubleField(6, value, defaultValue: nil)
70-
case let .dictionary(values):
70+
case .dictionary(let values):
7171
for item in values {
7272
try encoder.messageField(7, KeyValue(key: item.key, value: item.value))
7373
}
74-
case let .array(values):
74+
case .array(let values):
7575
for item in values {
7676
try encoder.messageField(7, item)
7777
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)