Skip to content

Commit 122c308

Browse files
zhu-xiaoweizxkane
authored andcommitted
feat: save and send event
1 parent 8654aa6 commit 122c308

File tree

48 files changed

+1826
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1826
-247
lines changed

.gitlab-ci.yml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
default:
2-
image: swift
32
tags:
4-
- size:2xlarge
5-
- arch:amd64
3+
- instance-type:mac2.metal
64

75
workflow:
86
rules:
@@ -13,12 +11,42 @@ workflow:
1311
- when: never
1412

1513
# Check merge request message
16-
lint:
17-
stage: build
14+
title-lint:
15+
stage: .pre
1816
script:
1917
- echo Start PR Validation
2018
- env
2119
- '[[ "$CI_COMMIT_MESSAGE" =~ ^(feat|fix|chore|docs|tests|ci) ]] || (echo "no commit type is specified in merge request title" && exit 1)'
2220

21+
# Check code lint issue
22+
code-lint:
23+
stage: .pre
24+
script:
25+
- swiftlint
2326

27+
# Check code format issue
28+
code-format:
29+
stage: .pre
30+
script:
31+
- swiftformat .
2432

33+
# Execute code build
34+
build:
35+
stage: build
36+
script:
37+
- swift build
38+
39+
# Execute code test
40+
test:
41+
stage: test
42+
script:
43+
- rm -rf .build/test/
44+
- xcodebuild test -scheme aws-solution-clickstream-swift -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=latest' -resultBundlePath .build/test/TestResults.xcresult
45+
- xcrun xccov view --report --json .build/test/TestResults.xcresult > .build/test/report.json
46+
- COVERAGE_EXPRESS=$(grep -Eo '"lineCoverage":\s*([0-9]+\.[0-9]+)(?:,"targets":)' .build/test/report.json)
47+
- COVERAGE=$(echo "$COVERAGE_EXPRESS" | grep -o '[0-9]*\.[0-9]*' | awk '{ printf "%.2f", $1 * 100 }')
48+
- echo "coverage:$COVERAGE"
49+
coverage: '/coverage:([0-9]+\.[0-9]+)/'
50+
artifacts:
51+
paths:
52+
- .build/test/

.swiftlint.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
excluded:
66
- Pods
77
- .build
8+
- Tests
89

910
analyzer_rules:
1011
- unused_import
@@ -13,6 +14,9 @@ analyzer_rules:
1314
opt_in_rules:
1415
- empty_count
1516

17+
disabled_rules:
18+
- opening_brace
19+
1620
# configurable rules can be customized from this configuration file
1721
closing_brace: error
1822
colon:
@@ -28,10 +32,12 @@ identifier_name:
2832
- id
2933
- of
3034
- or
35+
allowed_symbols:
36+
- _
37+
validates_start_with_lowercase: false
3138
line_length:
3239
warning: 120
3340
error: 160
34-
opening_brace: error
3541
return_arrow_whitespace: error
3642
statement_position:
3743
severity: error
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1420"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "Clickstream"
18+
BuildableName = "Clickstream"
19+
BlueprintName = "Clickstream"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "ClickstreamTests"
32+
BuildableName = "ClickstreamTests"
33+
BlueprintName = "ClickstreamTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
buildConfiguration = "Debug"
41+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
42+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
43+
shouldUseLaunchSchemeArgsEnv = "NO"
44+
codeCoverageEnabled = "YES"
45+
onlyGenerateCoverageForSpecifiedTargets = "YES">
46+
<CommandLineArguments>
47+
<CommandLineArgument
48+
argument = "--enable-test-discovery"
49+
isEnabled = "YES">
50+
</CommandLineArgument>
51+
</CommandLineArguments>
52+
<CodeCoverageTargets>
53+
<BuildableReference
54+
BuildableIdentifier = "primary"
55+
BlueprintIdentifier = "Clickstream"
56+
BuildableName = "Clickstream"
57+
BlueprintName = "Clickstream"
58+
ReferencedContainer = "container:">
59+
</BuildableReference>
60+
</CodeCoverageTargets>
61+
<Testables>
62+
<TestableReference
63+
skipped = "NO">
64+
<BuildableReference
65+
BuildableIdentifier = "primary"
66+
BlueprintIdentifier = "ClickstreamTests"
67+
BuildableName = "ClickstreamTests"
68+
BlueprintName = "ClickstreamTests"
69+
ReferencedContainer = "container:">
70+
</BuildableReference>
71+
</TestableReference>
72+
</Testables>
73+
</TestAction>
74+
<LaunchAction
75+
buildConfiguration = "Debug"
76+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
77+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
78+
launchStyle = "0"
79+
useCustomWorkingDirectory = "NO"
80+
ignoresPersistentStateOnLaunch = "NO"
81+
debugDocumentVersioning = "YES"
82+
debugServiceExtension = "internal"
83+
allowLocationSimulation = "YES">
84+
</LaunchAction>
85+
<ProfileAction
86+
buildConfiguration = "Release"
87+
shouldUseLaunchSchemeArgsEnv = "YES"
88+
savedToolIdentifier = ""
89+
useCustomWorkingDirectory = "NO"
90+
debugDocumentVersioning = "YES">
91+
<MacroExpansion>
92+
<BuildableReference
93+
BuildableIdentifier = "primary"
94+
BlueprintIdentifier = "Clickstream"
95+
BuildableName = "Clickstream"
96+
BlueprintName = "Clickstream"
97+
ReferencedContainer = "container:">
98+
</BuildableReference>
99+
</MacroExpansion>
100+
</ProfileAction>
101+
<AnalyzeAction
102+
buildConfiguration = "Debug">
103+
</AnalyzeAction>
104+
<ArchiveAction
105+
buildConfiguration = "Release"
106+
revealArchiveInOrganizer = "YES">
107+
</ArchiveAction>
108+
</Scheme>

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@ let package = Package(
1010
products: [
1111
.library(
1212
name: "Clickstream",
13-
targets: ["Clickstream"])
13+
targets: ["Clickstream"]
14+
)
1415
],
1516
dependencies: [
16-
.package(url: "https://github.com/aws-amplify/amplify-swift.git", exact: "2.4.0")
17+
.package(url: "https://github.com/aws-amplify/amplify-swift.git", exact: "2.4.0"),
18+
.package(url: "https://github.com/stephencelis/SQLite.swift.git", exact: "0.13.2"),
19+
.package(url: "https://github.com/1024jp/GzipSwift", exact: "5.2.0"),
20+
.package(url: "https://github.com/httpswift/swifter", exact: "1.5.0")
1721
],
1822
targets: [
1923
.target(
2024
name: "Clickstream",
2125
dependencies: [
22-
.product(name: "AWSPluginsCore", package: "amplify-swift")
23-
]),
26+
.product(name: "AWSPluginsCore", package: "amplify-swift"),
27+
.product(name: "SQLite", package: "SQLite.swift"),
28+
.product(name: "Gzip", package: "GzipSwift")
29+
]
30+
),
2431
.testTarget(
2532
name: "ClickstreamTests",
26-
dependencies: ["Clickstream"])
27-
])
33+
dependencies: ["Clickstream", .product(name: "Swifter", package: "swifter")]
34+
)
35+
]
36+
)

Sources/Clickstream/AWSClickstreamPlugin+ClientBehavior.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public extension AWSClickstreamPlugin {
1212
func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
1313
Task {
1414
if userId == Event.User.USER_ID_NIL {
15-
await analyticsClient.removeUserAttribute(forKey: userId)
15+
await analyticsClient.removeUserAttribute(forKey: Event.ReservedAttribute.USER_ID)
1616
} else if userId != Event.User.USER_ID_EMPTY {
1717
await analyticsClient.addUserAttribute(userId, forKey: Event.ReservedAttribute.USER_ID)
1818
}
@@ -25,13 +25,17 @@ public extension AWSClickstreamPlugin {
2525
}
2626

2727
func record(event: AnalyticsEvent) {
28+
guard let event = event as? BaseClickstreamEvent else{
29+
log.error("Event type does not match")
30+
return
31+
}
2832
if !isEnabled {
2933
log.warn("Cannot record events. Clickstream is disabled")
3034
return
3135
}
3236
let clickstreamEvent = analyticsClient.createEvent(withEventType: event.name)
33-
if let properties = event.properties {
34-
clickstreamEvent.addProperties(properties)
37+
if let attributes = event.attribute {
38+
clickstreamEvent.addAttribute(attributes)
3539
}
3640

3741
Task {
@@ -44,7 +48,7 @@ public extension AWSClickstreamPlugin {
4448
}
4549

4650
func record(eventWithName eventName: String) {
47-
let event = BasicAnalyticsEvent(name: eventName)
51+
let event = BaseClickstreamEvent(name: eventName)
4852
record(event: event)
4953
}
5054

Sources/Clickstream/AWSClickstreamPlugin+Configure.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public extension AWSClickstreamPlugin {
2727
let contextConfiguration = ClickstreamContextConfiguration(appId: configuration.appId,
2828
endpoint: configuration.endpoint,
2929
sendEventsInterval: configuration.sendEventsInterval,
30-
isTrackAppExceptionEvents: configuration.isTrackAppExceptionEvents,
30+
isTrackAppExceptionEvents:
31+
configuration.isTrackAppExceptionEvents,
3132
isCompressEvents: configuration.isCompressEvents)
32-
let clickstream = try ClickstreamContext(with: contextConfiguration)
33+
clickstream = try ClickstreamContext(with: contextConfiguration)
3334

3435
let sessionClient = SessionClient(configuration: .init(uniqueDeviceId: clickstream.uniqueId,
3536
sessionBackgroundTimeout: TimeInterval(10)),
@@ -52,7 +53,7 @@ public extension AWSClickstreamPlugin {
5253

5354
var autoFlushEventsTimer: DispatchSourceTimer?
5455
if configuration.sendEventsInterval != 0 {
55-
let timeInterval = TimeInterval(configuration.sendEventsInterval / 1000)
56+
let timeInterval = TimeInterval(Double(configuration.sendEventsInterval) / 1_000)
5657
autoFlushEventsTimer = RepeatingTimer.createRepeatingTimer(
5758
timeInterval: timeInterval,
5859
eventHandler: { [weak self] in
@@ -62,21 +63,21 @@ public extension AWSClickstreamPlugin {
6263
)
6364
}
6465

65-
configure(clickstream: clickstream,
66-
autoFlushEventsTimer: autoFlushEventsTimer,
67-
networkMonitor: networkMonitor)
66+
configure(
67+
autoFlushEventsTimer: autoFlushEventsTimer,
68+
networkMonitor: networkMonitor
69+
)
6870
sessionClient.startSession()
6971
log.debug("init the sdk success")
7072
}
7173

7274
// MARK: Internal
7375

7476
/// Internal configure method to set the properties of the plugin
75-
internal func configure(clickstream: ClickstreamContext,
76-
autoFlushEventsTimer: DispatchSourceTimer?,
77-
networkMonitor: NetworkMonitor)
78-
{
79-
self.clickstream = clickstream
77+
internal func configure(
78+
autoFlushEventsTimer: DispatchSourceTimer?,
79+
networkMonitor: NetworkMonitor
80+
) {
8081
isEnabled = true
8182
self.autoFlushEventsTimer = autoFlushEventsTimer
8283
self.autoFlushEventsTimer?.resume()

Sources/Clickstream/AWSClickstreamPlugin+DefaultLogger.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8-
import Amplify
9-
10-
extension AWSClickstreamPlugin: DefaultLogger {}
8+
extension AWSClickstreamPlugin: ClickstreamLogger {}

Sources/Clickstream/AWSClickstreamPlugin+Reset.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public extension AWSClickstreamPlugin {
1818
}
1919

2020
if isEnabled != nil {
21-
isEnabled = nil
21+
isEnabled = false
2222
}
2323

2424
if networkMonitor != nil {

0 commit comments

Comments
 (0)