|
2 | 2 |
|
3 | 3 | ## Introduction |
4 | 4 |
|
5 | | -Clickstream iOS SDK can help you easily report in-app events on iOS. After the event is reported, statistics and analysis of specific scenario data can be completed on AWS Clickstream solution. |
| 5 | +Clickstream Swift SDK can help you easily report in-app events on iOS. After the event is reported, statistics and analysis of specific scenario data can be completed on AWS Clickstream solution. |
6 | 6 |
|
7 | 7 | The SDK relies on the Amplify for Swift Core Library and is developed according to the Amplify Swift SDK plug-in specification. In addition to this, we've added commonly used preset event statistics to make it easier to use. |
8 | 8 |
|
9 | | -## Platform Support |
| 9 | +### Platform Support |
10 | 10 |
|
11 | 11 | The Clickstream SDK supports iOS 13+. |
12 | 12 |
|
| 13 | +## Integrate SDK |
| 14 | + |
| 15 | +Clickstream requires Xcode 13.4 or higher to build. |
| 16 | + |
| 17 | +**1.Add Package** |
| 18 | + |
| 19 | +We use **Swift Package Manager** to distribute Clickstream Swift SDK, open your project in Xcode and select **File > Add Pckages**. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Enter the Clickstream Library for Swift GitHub repo URL (`https://github.com/awslabs/clickstream-swift`) into the search bar, You'll see the Clickstream Library for Swift repository rules for which version of Clickstream you want Swift Package Manager to install. Choose **Up to Next Major Version**, then click **Add Package**, make the Clickstream product checked as default, and click **Add Package** again. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +**2.Parameter configuration** |
| 28 | + |
| 29 | +Downlod your `amplifyconfiguration.json` file from your Clickstream solution control plane, and paste it to your project root folder: |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +the json file will be as follows: |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "analytics": { |
| 38 | + "plugins": { |
| 39 | + "awsClickstreamPlugin ": { |
| 40 | + "appId": "appId", |
| 41 | + "endpoint": "https://example.com/collect", |
| 42 | + "isCompressEvents": true, |
| 43 | + "autoFlushEventsInterval": 10000, |
| 44 | + "isTrackAppExceptionEvents": false |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Your `appId` and `endpoint` are already set up in it, here's an explanation of each property: |
| 52 | + |
| 53 | +- **appId**: the app id of your project in control plane. |
| 54 | +- **endpoint**: the endpoint url you will upload the event to AWS server. |
| 55 | +- **isCompressEvents**: whether to compress event content when uploading events, default is `true` |
| 56 | +- **autoFlushEventsInterval**: event sending interval, the default is `10s` |
| 57 | +- **isTrackAppExceptionEvents**: whether auto track exception event in app, default is `false` |
| 58 | + |
| 59 | +**3.Initialize the SDK** |
| 60 | + |
| 61 | +Once you have configured the parameters, you need to initialize it in AppDelegate's `didFinishLaunchingWithOptions` lifecycle method to use the SDK. |
| 62 | + |
| 63 | +```swift |
| 64 | +import Clickstream |
| 65 | +... |
| 66 | +func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 67 | + // Override point for customization after application launch. |
| 68 | + do { |
| 69 | + try ClickstreamAnalytics.initSDK() |
| 70 | + } catch { |
| 71 | + assertionFailure("Fail to initialize ClickstreamAnalytics: \(error)") |
| 72 | + } |
| 73 | + return true |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +**4.Config the SDK** |
| 78 | + |
| 79 | +```swift |
| 80 | +import Clickstream |
| 81 | + |
| 82 | +// config the sdk after initialize. |
| 83 | +do { |
| 84 | + var configuration = try ClickstreamAnalytics.getClickstreamConfiguration() |
| 85 | + configuration.appId = "appId" |
| 86 | + configuration.endpoint = "https://example.com/collect" |
| 87 | + configuration.authCookie = "your authentication cookie" |
| 88 | + configuration.sessionTimeoutDuration = 1800000 |
| 89 | + configuration.isTrackAppExceptionEvents = false |
| 90 | + configuration.isLogEvents = true |
| 91 | + configuration.isCompressEvents = true |
| 92 | + configuration.isLogEvents = true |
| 93 | +} catch { |
| 94 | + print("Failed to config ClickstreamAnalytics: \(error)") |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +> note: this configuation will override the default configuation in `amplifyconfiguration.json` file |
| 99 | +
|
| 100 | +### Start using |
| 101 | + |
| 102 | +#### Recored event. |
| 103 | + |
| 104 | +Add the following code where you need to complete the event report. |
| 105 | + |
| 106 | +```swift |
| 107 | +import Clickstream |
| 108 | + |
| 109 | +let attributes: ClickstreamAttribute = [ |
| 110 | + "channel": "apple", |
| 111 | + "uccessful": true, |
| 112 | + "ProcessDuration": 12.33, |
| 113 | + "UserAge": 20, |
| 114 | +] |
| 115 | +ClickstreamAnalytics.recordEvent(eventName: "testEvent", attributes: attributes) |
| 116 | + |
| 117 | +// for record an event directly |
| 118 | +ClickstreamAnalytics.recordEvent(eventName: "button_click") |
| 119 | +``` |
| 120 | + |
| 121 | +#### Add global attribute |
| 122 | + |
| 123 | +```swift |
| 124 | +import Clickstream |
| 125 | + |
| 126 | +let globalAttribute: ClickstreamAttribute = [ |
| 127 | + "channel": "apple", |
| 128 | + "class": 6, |
| 129 | + "level": 5.1, |
| 130 | + "isOpenNotification": true, |
| 131 | +] |
| 132 | +ClickstreamAnalytics.addGlobalAttributes(attributes: globalAttribute) |
| 133 | + |
| 134 | +// for delete an global attribute |
| 135 | +ClickstreamAnalytics.deleteGlobalAttributes(attributes: "level") |
| 136 | +``` |
| 137 | + |
| 138 | +#### Login and logout |
| 139 | + |
| 140 | +```swift |
| 141 | +import Clickstream |
| 142 | + |
| 143 | +// when user login usccess. |
| 144 | +ClickstreamAnalytics.setUserId(userId: "userId") |
| 145 | + |
| 146 | +// when user logout |
| 147 | +ClickstreamAnalytics.setUserId(userId: nil) |
| 148 | +``` |
| 149 | + |
| 150 | +When we log into another user, we will clear the before user's user attributes, after `setUserId()` you need to add new user's attribute. |
| 151 | + |
| 152 | +#### Add user attribute |
| 153 | + |
| 154 | +```swift |
| 155 | +import Clickstream |
| 156 | + |
| 157 | +let userAttributes : ClickstreamAttribute=[ |
| 158 | + "_user_age": 21, |
| 159 | + "_user_name": "carl" |
| 160 | +] |
| 161 | +ClickstreamAnalytics.addUserAttributes(attributes: userAttributes) |
| 162 | +``` |
| 163 | + |
| 164 | +Current login user‘s attributes will be cached in disk, so the next time app launch you don't need to set all user's attribute again, of course you can update the current user's attribute when it changes. |
| 165 | + |
| 166 | +#### Log the event json in debug mode |
| 167 | + |
| 168 | +```swift |
| 169 | +import Clickstream |
| 170 | + |
| 171 | +// log the event in debug mode. |
| 172 | +do { |
| 173 | + var configuration = try ClickstreamAnalytics.getClickstreamConfiguration() |
| 174 | + configuration.isLogEvents = true |
| 175 | +} catch { |
| 176 | + print("Failed to config ClickstreamAnalytics: \(error)") |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +After config `configuration.isLogEvents = true` and when you record an event, you can see the event json at your Xcode log pannel by filter `EventRecorder`. |
| 181 | + |
| 182 | +#### Send event immediately |
| 183 | + |
| 184 | +```swift |
| 185 | +import Clickstream |
| 186 | +// for send event immediately. |
| 187 | +ClickstreamAnalytics.flushEvents() |
| 188 | +``` |
| 189 | + |
13 | 190 | ## How to build&test locally |
14 | 191 |
|
15 | 192 | ### Config your code format |
|
0 commit comments