Skip to content

Commit 3260f1e

Browse files
first commit
1 parent a4b2ac5 commit 3260f1e

File tree

6 files changed

+192
-73
lines changed

6 files changed

+192
-73
lines changed

README.md

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
1-
# android-example
1+
# Metricalp Official Android Library
22

3-
[![Release](https://jitpack.io/v/jitpack/android-example.svg)](https://jitpack.io/#jitpack/android-example)
4-
5-
Example Android library project that works with jitpack.io.
6-
7-
See this [Tutorial](https://medium.com/@ome450901/publish-an-android-library-by-jitpack-a0342684cbd0) on how to publish an Android Library with JitPack.
8-
9-
For more details check out the [documentation](https://github.com/jitpack/jitpack.io/blob/master/ANDROID.md)
10-
11-
https://jitpack.io/#jitpack/android-example
12-
13-
Add it to your build.gradle with:
14-
```gradle
15-
allprojects {
16-
repositories {
17-
maven { url "https://jitpack.io" }
18-
}
19-
}
20-
```
21-
and:
22-
23-
```gradle
24-
dependencies {
25-
compile 'com.github.jitpack:android-example:{latest version}'
26-
}
27-
```
28-
29-
## Multiple build variants
30-
31-
If your library uses multiple flavours then see this example:
32-
https://github.com/jitpack-io/android-jitpack-library-example
33-
34-
## Adding the maven plugin
35-
36-
To enable installing into local maven repository and JitPack you need to add the [android-maven](https://github.com/dcendents/android-maven-gradle-plugin) plugin:
37-
38-
1. Add `classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'` to root build.gradle under `buildscript { dependencies {`
39-
2. Add `com.github.dcendents.android-maven` to the library/build.gradle
40-
41-
After these changes you should be able to run:
42-
43-
./gradlew install
44-
45-
from the root of your project. If install works and you have added a GitHub release it should work on jitpack.io
46-
47-
## Adding a sample app
48-
49-
If you add a sample app to the same repo then your app needs to have a dependency on the library. To do this in your app/build.gradle add:
50-
51-
```gradle
52-
dependencies {
53-
compile project(':library')
54-
}
55-
```
3+
For more details check out the [documentation](https://www.metricalp.com/docs/android)

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
buildscript {
2-
ext.kotlin_version = "1.6.10"
32
repositories {
43
google()
54
mavenCentral()
65
}
76
dependencies {
87
classpath 'com.android.tools.build:gradle:4.2.2'
9-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
108
}
119
}
1210

library/build.gradle

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
plugins {
22
id 'com.android.library'
3-
id 'kotlin-android'
43
id 'maven-publish'
54
}
65

7-
group = 'com.github.jitpack'
6+
group = 'com.github.metricalp'
87
version = '1.0'
98

109
android {
11-
compileSdkVersion 31
10+
compileSdkVersion 34
1211

1312
defaultConfig {
1413
minSdkVersion 21
15-
targetSdkVersion 31
14+
targetSdkVersion 33
1615
versionCode 1
1716
versionName "1.0"
1817
}
@@ -24,16 +23,12 @@ android {
2423
}
2524
}
2625

27-
kotlinOptions {
28-
jvmTarget = '1.8'
29-
}
3026
}
3127

3228
dependencies {
33-
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
34-
implementation 'androidx.core:core-ktx:1.7.0'
3529
implementation 'androidx.appcompat:appcompat:1.4.1'
36-
implementation 'com.google.android.material:material:1.5.0'
30+
implementation 'com.google.code.gson:gson:2.10.1'
31+
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
3732
}
3833

3934
afterEvaluate {
@@ -42,8 +37,8 @@ afterEvaluate {
4237
// Creates a Maven publication called "release".
4338
release(MavenPublication) {
4439
from components.release
45-
groupId = 'com.github.jitpack'
46-
artifactId = 'android-example'
40+
groupId = 'com.github.metricalp'
41+
artifactId = 'android'
4742
version = '1.0'
4843
}
4944
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<manifest package="io.jitpack.api">
2-
3-
<application />
4-
5-
</manifest>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<uses-permission android:name="android.permission.INTERNET"/>
4+
</manifest>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import android.os.Build;
2+
3+
4+
import com.google.gson.Gson;
5+
import com.google.gson.reflect.TypeToken;
6+
7+
8+
import java.io.IOException;
9+
import java.lang.reflect.Type;
10+
import java.util.HashMap;
11+
12+
import okhttp3.Call;
13+
import okhttp3.Callback;
14+
import okhttp3.MediaType;
15+
import okhttp3.OkHttpClient;
16+
import okhttp3.Request;
17+
import okhttp3.RequestBody;
18+
import okhttp3.Response;
19+
20+
21+
public final class Metricalp {
22+
23+
private static Metricalp INSTANCE;
24+
25+
public final String API_ENDPOINT = "https://event.metricalp.com";
26+
private HashMap<String, String> attributes = null;
27+
28+
public OkHttpClient client = new OkHttpClient();
29+
30+
private Metricalp() {
31+
}
32+
33+
public static Metricalp getInstance() {
34+
if (INSTANCE == null) {
35+
INSTANCE = new Metricalp();
36+
}
37+
38+
return INSTANCE;
39+
}
40+
41+
public static boolean init(HashMap<String, String> attributes, String initialScreen) {
42+
Metricalp instance = Metricalp.getInstance();
43+
if (instance.getAttributes() == null) {
44+
attributes.put("metr_collected_via", "android");
45+
attributes.put("metr_os_detail", "Android " + Build.VERSION.SDK_INT + "_" + Build.VERSION.RELEASE);
46+
attributes.put("metr_app_detail", attributes.getOrDefault("app", "(not-set)"));
47+
instance.setAttributes(attributes);
48+
}
49+
if (initialScreen == null) {
50+
return true;
51+
}
52+
53+
return Metricalp.screenViewEvent(initialScreen, null, null);
54+
}
55+
56+
public HashMap<String, String> getAttributes() {
57+
return attributes;
58+
}
59+
60+
public void setAttributes(HashMap<String, String> attributes) {
61+
this.attributes = attributes;
62+
}
63+
64+
65+
public static void resetAttributes(HashMap<String, String> attributes) {
66+
Metricalp instance = Metricalp.getInstance();
67+
instance.setAttributes(attributes);
68+
}
69+
70+
public static void updateAttributes(HashMap<String, String> attributes) {
71+
Metricalp instance = Metricalp.getInstance();
72+
HashMap<String, String> currentAttributes = instance.getAttributes();
73+
currentAttributes.putAll(attributes);
74+
instance.setAttributes(currentAttributes);
75+
}
76+
77+
public static HashMap<String, String> getAllAttributes() {
78+
Metricalp instance = Metricalp.getInstance();
79+
return instance.getAttributes();
80+
}
81+
82+
public static boolean sendEvent(String type, HashMap<String, String> eventAttributes, HashMap<String, String> overrideAttributes) {
83+
Metricalp instance = Metricalp.getInstance();
84+
HashMap<String, String> attributes = instance.getAttributes();
85+
HashMap<String, String> body = new HashMap<>(attributes);
86+
87+
body.putAll(attributes);
88+
89+
if (overrideAttributes != null) {
90+
body.putAll(overrideAttributes);
91+
}
92+
93+
if (!body.containsKey("tid")) {
94+
throw new RuntimeException("Metricalp: tid is missing in attributes");
95+
}
96+
97+
if (body.containsKey("bypassIpUniqueness") && !body.containsKey("metr_unique_identifier")) {
98+
throw new RuntimeException("Metricalp: when bypassIpUniqueness is true, metr_unique_identifier must be set.");
99+
}
100+
101+
102+
if (eventAttributes != null) {
103+
body.putAll(eventAttributes);
104+
body.put("path", eventAttributes.getOrDefault("path", "(not-set)"));
105+
}
106+
107+
body.put("type", type);
108+
109+
110+
if (!body.containsKey("metr_user_language")) {
111+
body.put("metr_user_language", "unknown-unknown");
112+
}
113+
114+
if (!body.containsKey("metr_unique_identifier")) {
115+
body.put("metr_unique_identifier", "");
116+
}
117+
118+
119+
String apiUrl = attributes.getOrDefault("endpoint", instance.API_ENDPOINT);
120+
Gson gson = new Gson();
121+
Type typeObject = new TypeToken<HashMap>() {
122+
}.getType();
123+
String gsonData = gson.toJson(body, typeObject);
124+
125+
instance.asynchronousPostRequest(apiUrl, gsonData);
126+
127+
return true;
128+
}
129+
130+
public static boolean screenViewEvent(String path, HashMap<String, String> eventAttributes, HashMap<String, String> overrideAttributes) {
131+
HashMap<String, String> attrs = new HashMap<>();
132+
attrs.put("path", path);
133+
if (eventAttributes != null) {
134+
attrs.putAll(eventAttributes);
135+
}
136+
return Metricalp.sendEvent(
137+
"screen_view",
138+
attrs,
139+
overrideAttributes
140+
);
141+
}
142+
143+
public static boolean sessionExitEvent(String path, HashMap<String, String> eventAttributes, HashMap<String, String> overrideAttributes) {
144+
HashMap<String, String> attrs = new HashMap<>();
145+
attrs.put("path", path);
146+
if (eventAttributes != null) {
147+
attrs.putAll(eventAttributes);
148+
}
149+
return Metricalp.sendEvent(
150+
"session_exit",
151+
attrs,
152+
overrideAttributes
153+
);
154+
}
155+
156+
public static boolean customEvent(String type, HashMap<String, String> eventAttributes, HashMap<String, String> overrideAttributes) {
157+
return Metricalp.sendEvent(
158+
type,
159+
eventAttributes,
160+
overrideAttributes
161+
);
162+
}
163+
164+
public void asynchronousPostRequest(String url, String requestBody) {
165+
RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json"));
166+
Request request = new Request.Builder()
167+
.url(url)
168+
.post(body)
169+
.build();
170+
this.client.newCall(request).enqueue(new Callback() {
171+
172+
@Override
173+
public void onFailure(Call call, IOException e) {}
174+
175+
@Override
176+
public void onResponse(Call call, Response response) {}
177+
});
178+
}
179+
}

library/src/main/kotlin/io/jitpack/api/JitPack.kt

Whitespace-only changes.

0 commit comments

Comments
 (0)