Skip to content

Commit 42dfd3f

Browse files
zhu-xiaoweixiaoweii
andauthored
chore: update readme with usage guide (#13)
1. update readme for usage doc 2. fix publishing.gradle with no keyFile. --------- Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 923502d commit 42dfd3f

File tree

4 files changed

+204
-26
lines changed

4 files changed

+204
-26
lines changed

README.md

Lines changed: 195 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# AWS Solution Clickstream Analytics SDK for Android
2-
2+
[![Maven Central](https://img.shields.io/maven-central/v/software.aws.solution/clickstream.svg)](https://search.maven.org/artifact/software.aws.solution/clickstream)
33
## Introduction
4+
45
Clickstream Android SDK can help you easily report in-app events on Android. After the event is reported, statistics and analysis of specific scenario data can be completed on AWS Clickstream solution.
56

67
The SDK relies on the Amplify for Android SDK Core Library and is developed according to the Amplify Android SDK plug-in specification, while using the same event definitions and attribute specifications as amplifyframework analytics. In addition to this, we've added commonly used preset event statistics to make it easier to use.
@@ -9,26 +10,200 @@ The SDK relies on the Amplify for Android SDK Core Library and is developed acco
910

1011
The Clickstream SDK supports Android API level 16 (Android 4.1) and above.
1112

13+
## Integrate SDK
14+
15+
**1.Include SDK**
16+
17+
Add the following dependency to your `app` module's `build.gradle` file.
18+
19+
```groovy
20+
dependencies {
21+
implementation 'software.aws.solution:clickstream:0.4.1'
22+
}
23+
```
24+
25+
then sync your project.
26+
27+
**2.Parameter configuration**
28+
29+
Find the res directory under your `project/app/src/main` , and manually create a raw folder in the res directory.
30+
31+
![](images/raw_folder.png)
32+
33+
Downlod your `amplifyconfiguration.json` file from your clickstream control plane, and paste it to raw floder, the json file will be as following:
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 `true`
58+
59+
**3.Initialize the SDK**
60+
61+
Please Initialize the SDK in the Application `onCreate()` method.
62+
63+
```java
64+
import software.aws.solution.clickstream.ClickstreamAnalytics;
65+
66+
public void onCreate() {
67+
super.onCreate();
68+
69+
try{
70+
ClickstreamAnalytics.init(this);
71+
Log.i("MyApp", "Initialized ClickstreamAnalytics");
72+
} catch (AmplifyException error){
73+
Log.e("MyApp", "Could not initialize ClickstreamAnalytics", error);
74+
}
75+
}
76+
```
77+
78+
**4.Config the SDK**
79+
80+
After initial the SDK we can use the following code to custom configure it.
81+
82+
```java
83+
import software.aws.solution.clickstream.ClickstreamAnalytics;
84+
85+
// config the SDK after initialize.
86+
ClickstreamAnalytics.getClickStreamConfiguration()
87+
.withAppId("appId")
88+
.withEndpoint("https://example.com/collect")
89+
.withAuthCookie("your authentication cookie")
90+
.withSendEventsInterval(10000)
91+
.withSessionTimeoutDuration(1800000)
92+
.withTrackAppExceptionEvents(false)
93+
.withLogEvents(true)
94+
.withCustomDns(CustomOkhttpDns.getInstance())
95+
.withCompressEvents(true);
96+
```
97+
98+
> note: this configuration will override the default configuration in `amplifyconfiguration.json` file.
99+
100+
### Start using
101+
102+
Now that you've integrated the SDK, let's start using it in your app.
103+
104+
#### Record event
105+
106+
Add the following code where you need to report an event.
107+
108+
```java
109+
import software.aws.solution.clickstream.ClickstreamAnalytics;
110+
import com.amplifyframework.analytics.AnalyticsEvent;
111+
112+
AnalyticsEvent event = AnalyticsEvent.builder()
113+
.name("PasswordReset")
114+
.add("Channel", "SMS")
115+
.add("Successful", true)
116+
.add("ProcessDuration", 78.2)
117+
.add("UserAge", 20)
118+
.build();
119+
ClickstreamAnalytics.recordEvent(event);
120+
121+
// for record an event directly
122+
ClickstreamAnalytics.recordEvent("button_click");
123+
```
124+
125+
#### Add global attribute
126+
127+
```java
128+
import software.aws.solution.clickstream.ClickstreamAttribute;
129+
import software.aws.solution.clickstream.ClickstreamAnalytics;
130+
131+
ClickstreamAttribute globalAttribute = ClickstreamAttribute.builder()
132+
.add("channel", "HUAWEI")
133+
.add("level", 5.1)
134+
.add("class", 6)
135+
.add("isOpenNotification", true)
136+
.build();
137+
ClickstreamAnalytics.addGlobalAttributes(globalAttribute);
138+
139+
// for delete an global attribute
140+
ClickstreamAnalytics.deleteGlobalAttributes("level");
141+
```
142+
143+
#### Login and logout
144+
145+
```java
146+
import software.aws.solution.clickstream.ClickstreamAnalytics;
147+
148+
// when user login success.
149+
ClickstreamAnalytics.setUserId("UserId");
150+
151+
// when user logout
152+
ClickstreamAnalytics.setUserId(null);
153+
```
154+
155+
When we log into another user, we will clear the before user's user attributes, after `setUserId()` you need add your user's attribute.
156+
157+
#### Add user attribute
158+
159+
```java
160+
import software.aws.solution.clickstream.ClickstreamAnalytcs;
161+
import software.aws.solution.clickstream.ClickstreamUserAttribute;
162+
163+
ClickstreamUserAttribute clickstreamUserAttribute = ClickstreamUserAttribute.builder()
164+
.add("_user_age", 21)
165+
.add("_user_name", "carl")
166+
.build();
167+
ClickstreamAnalytics.addUserAttributes(clickstreamUserAttribute);
168+
```
169+
170+
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.
171+
172+
#### Log the event json in debug mode
173+
174+
```java
175+
import software.aws.solution.clickstream.ClickstreamAnalytics;
176+
177+
// log the event in debug mode.
178+
ClickstreamAnalytics.getClickStreamConfiguration()
179+
.withLogEvents(BuildConfig.DEBUG);
180+
```
181+
182+
after config `.withLogEvents(true)` and when you record an event, you can see the event json at your AndroidStudio **Logcat** by filter `EventRecorder`.
183+
184+
#### Config custom DNS
185+
186+
```java
187+
import software.aws.solution.clickstream.ClickstreamAnalytics;
188+
189+
// config custom dns.
190+
ClickstreamAnalytics.getClickStreamConfiguration()
191+
.withCustomDns(CustomOkhttpDns.getInstance());
192+
```
193+
194+
If you want to use custom DNS for network request, you can create your `CustomOkhttpDns` which implementaion `okhttp3.Dns`, then config `.withCustomDns(CustomOkhttpDns.getInstance())` to make it works.
195+
196+
#### Send event immediately
197+
198+
```java
199+
// for send event immediately.
200+
ClickstreamAnalytics.flushEvent();
201+
```
202+
12203
## How to build locally
13-
### Config your local environment
14-
First of all you should install the latest version of [Android Studio](https://developer.android.com/studio).
15-
#### Config your checkstyle:
16-
1. Open your Android Studio -> Preferences -> Tools -> check style window.
17-
2. Change the check style version to 8.29.
18-
3. Add config file from ./configuration/checkstyle.gradle. then check and apply.
19-
20-
#### Config your code format
21-
1. Open your Android Studio -> Preferences -> Editor -> Code Style -> Java window.
22-
2. Click the top setting icon -> import scheme -> checkstyle configuration
23-
3. Select ./configuration/checkstyle.gradle file, then click ok to submit.
24-
4. Config your Reformat code keymap to format your code with checkstyle configured above.
25-
26-
#### Config your java version
27-
1. Open your Android Studio -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle window.
28-
2. make sure you `Gradle JDK` version is set to the 1.8, then click apply and ok.
29-
30-
### Build aar
31-
open an terminal window,at the root project folder to execute:
204+
205+
open an terminal window, at the root project folder to execute:
206+
32207
```shell
33208
./gradlew build -p clickstream
34209
```

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ subprojects { project ->
7575
afterEvaluate {
7676
configAndroidLibrary(project)
7777
project.apply from: '../jacoco.gradle'
78+
project.apply from: '../publishing.gradle'
7879
}
7980
}
8081

images/raw_folder.png

19.7 KB
Loading

publishing.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ version = project.ext.VERSION_NAME
2121
group = POM_GROUP
2222
def keyFile = rootProject.file("key.properties")
2323
def keyProp = new Properties()
24-
keyProp.load(keyFile.newInputStream())
24+
keyFile.exists() ? keyProp.load(keyFile.newInputStream()) : keyProp
2525

2626
afterEvaluate { project ->
2727
publishing {
@@ -89,10 +89,12 @@ afterEvaluate { project ->
8989
}
9090

9191
signing {
92-
def signingPassword = keyProp.getProperty("signing.password")
93-
def keyId = keyProp.getProperty("signing.keyId")
94-
def signingKey = keyProp.getProperty("signing.inMemoryKey").replace("\\n", "\n")
95-
useInMemoryPgpKeys(keyId, signingKey, signingPassword)
96-
sign publishing.publications.library
92+
if (keyFile.exists()) {
93+
def signingPassword = keyProp.getProperty("signing.password")
94+
def keyId = keyProp.getProperty("signing.keyId")
95+
def signingKey = keyProp.getProperty("signing.inMemoryKey").replace("\\n", "\n")
96+
useInMemoryPgpKeys(keyId, signingKey, signingPassword)
97+
sign publishing.publications.library
98+
}
9799
}
98100
}

0 commit comments

Comments
 (0)