Skip to content

Commit e5d103d

Browse files
author
Bas Buijsen
committed
setup analytics
1 parent d0fd99e commit e5d103d

File tree

17 files changed

+652
-0
lines changed

17 files changed

+652
-0
lines changed

firebase-analytics/build.gradle.kts

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
2+
3+
/*
4+
* Copyright (c) 2023 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
5+
*/
6+
7+
version = project.property("firebase-analytics.version") as String
8+
9+
plugins {
10+
id("com.android.library")
11+
kotlin("native.cocoapods")
12+
kotlin("multiplatform")
13+
}
14+
15+
android {
16+
val minSdkVersion: Int by project
17+
val compileSdkVersion: Int by project
18+
19+
compileSdk = compileSdkVersion
20+
namespace = "dev.gitlive.firebase.analytics"
21+
22+
defaultConfig {
23+
minSdk = minSdkVersion
24+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
25+
}
26+
27+
compileOptions {
28+
sourceCompatibility = JavaVersion.VERSION_11
29+
targetCompatibility = JavaVersion.VERSION_11
30+
}
31+
32+
testOptions {
33+
unitTests.apply {
34+
isIncludeAndroidResources = true
35+
}
36+
}
37+
packaging {
38+
resources.pickFirsts.add("META-INF/kotlinx-serialization-core.kotlin_module")
39+
resources.pickFirsts.add("META-INF/AL2.0")
40+
resources.pickFirsts.add("META-INF/LGPL2.1")
41+
}
42+
lint {
43+
abortOnError = false
44+
}
45+
}
46+
47+
val supportIosTarget = project.property("skipIosTarget") != "true"
48+
49+
kotlin {
50+
51+
targets.configureEach {
52+
compilations.configureEach {
53+
kotlinOptions.freeCompilerArgs += "-Xexpect-actual-classes"
54+
}
55+
}
56+
57+
@Suppress("OPT_IN_USAGE")
58+
androidTarget {
59+
instrumentedTestVariant.sourceSetTree.set(KotlinSourceSetTree.test)
60+
unitTestVariant.sourceSetTree.set(KotlinSourceSetTree.test)
61+
publishAllLibraryVariants()
62+
compilations.configureEach {
63+
kotlinOptions {
64+
jvmTarget = "11"
65+
}
66+
}
67+
}
68+
69+
jvm {
70+
compilations.getByName("main") {
71+
kotlinOptions {
72+
jvmTarget = "17"
73+
}
74+
}
75+
compilations.getByName("test") {
76+
kotlinOptions {
77+
jvmTarget = "17"
78+
}
79+
}
80+
}
81+
82+
if (supportIosTarget) {
83+
iosArm64()
84+
iosX64()
85+
iosSimulatorArm64()
86+
cocoapods {
87+
ios.deploymentTarget = "12.0"
88+
framework {
89+
baseName = "FirebaseAnalytics"
90+
}
91+
noPodspec()
92+
pod("FirebaseAnalytics") {
93+
version = "10.25.0"
94+
extraOpts += listOf("-compiler-option", "-fmodules")
95+
}
96+
}
97+
}
98+
99+
js(IR) {
100+
useCommonJs()
101+
nodejs {
102+
testTask(
103+
Action {
104+
useKarma {
105+
useChromeHeadless()
106+
}
107+
}
108+
)
109+
}
110+
browser {
111+
testTask(
112+
Action {
113+
useKarma {
114+
useChromeHeadless()
115+
}
116+
}
117+
)
118+
}
119+
}
120+
121+
sourceSets {
122+
all {
123+
languageSettings.apply {
124+
val apiVersion: String by project
125+
val languageVersion: String by project
126+
this.apiVersion = apiVersion
127+
this.languageVersion = languageVersion
128+
progressiveMode = true
129+
if (name.lowercase().contains("ios")) {
130+
optIn("kotlinx.cinterop.ExperimentalForeignApi")
131+
}
132+
}
133+
}
134+
135+
getByName("commonMain") {
136+
dependencies {
137+
api(project(":firebase-app"))
138+
implementation(project(":firebase-common"))
139+
}
140+
}
141+
142+
getByName("commonTest") {
143+
dependencies {
144+
implementation(project(":test-utils"))
145+
}
146+
}
147+
148+
getByName("androidMain") {
149+
dependencies {
150+
api("com.google.firebase:firebase-analytics")
151+
}
152+
}
153+
}
154+
}
155+
156+
if (project.property("firebase-analytics.skipIosTests") == "true") {
157+
tasks.forEach {
158+
if (it.name.contains("ios", true) && it.name.contains("test", true)) { it.enabled = false }
159+
}
160+
}
161+
162+
if (project.property("firebase-analytics.skipJsTests") == "true") {
163+
tasks.forEach {
164+
if (it.name.contains("js", true) && it.name.contains("test", true)) { it.enabled = false }
165+
}
166+
}
167+
168+
signing {
169+
val signingKey: String? by project
170+
val signingPassword: String? by project
171+
useInMemoryPgpKeys(signingKey, signingPassword)
172+
sign(publishing.publications)
173+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Pod::Spec.new do |spec|
2+
spec.name = 'firebase_analytics'
3+
spec.version = '1.8.1'
4+
spec.homepage = ''
5+
spec.source = { :http=> ''}
6+
spec.authors = ''
7+
spec.license = ''
8+
spec.summary = ''
9+
spec.vendored_frameworks = 'build/cocoapods/framework/firebase_analytics.framework'
10+
spec.libraries = 'c++'
11+
12+
13+
14+
spec.pod_target_xcconfig = {
15+
'KOTLIN_PROJECT_PATH' => ':firebase-analytics',
16+
'PRODUCT_MODULE_NAME' => 'firebase_analytics',
17+
}
18+
19+
spec.script_phases = [
20+
{
21+
:name => 'Build firebase_analytics',
22+
:execution_position => :before_compile,
23+
:shell_path => '/bin/sh',
24+
:script => <<-SCRIPT
25+
if [ "YES" = "$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED" ]; then
26+
echo "Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \"YES\""
27+
exit 0
28+
fi
29+
set -ev
30+
REPO_ROOT="$PODS_TARGET_SRCROOT"
31+
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
32+
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
33+
-Pkotlin.native.cocoapods.archs="$ARCHS" \
34+
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
35+
SCRIPT
36+
}
37+
]
38+
39+
end

firebase-analytics/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@gitlive/firebase-analytics",
3+
"version": "1.12.0",
4+
"description": "Wrapper around firebase for usage in Kotlin Multiplatform projects",
5+
"main": "firebase-analytics.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/GitLiveApp/firebase-kotlin-sdk.git"
12+
},
13+
"keywords": [
14+
"kotlin",
15+
"multiplatform",
16+
"kotlin-js",
17+
"firebase"
18+
],
19+
"author": "dev.gitlive",
20+
"license": "Apache-2.0",
21+
"bugs": {
22+
"url": "https://github.com/GitLiveApp/firebase-kotlin-sdk/issues"
23+
},
24+
"homepage": "https://github.com/GitLiveApp/firebase-kotlin-sdk",
25+
"dependencies": {
26+
"@gitlive/firebase-app": "1.12.0",
27+
"firebase": "9.19.1",
28+
"kotlin": "1.6.10",
29+
"kotlinx-coroutines-core": "1.6.1-native-mt"
30+
}
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
3+
<application android:usesCleartextTraffic="true" />
4+
</manifest>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:JvmName("tests")
6+
package dev.gitlive.firebase.analytics
7+
8+
import androidx.test.platform.app.InstrumentationRegistry
9+
10+
actual val emulatorHost: String = "10.0.2.2"
11+
12+
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
13+
14+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
15+
actual annotation class IgnoreForAndroidUnitTest
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dev.gitlive.firebase.analytics
2+
3+
import android.os.Bundle
4+
import com.google.firebase.analytics.analytics
5+
import dev.gitlive.firebase.Firebase
6+
import kotlinx.coroutines.tasks.await
7+
8+
actual val Firebase.analytics: FirebaseAnalytics
9+
get() = FirebaseAnalytics(com.google.firebase.Firebase.analytics)
10+
11+
actual class FirebaseAnalytics(val android: com.google.firebase.analytics.FirebaseAnalytics) {
12+
actual fun logEvent(name: String, parameters: Map<String, String>) {
13+
android.logEvent(name, parameters.toBundle())
14+
}
15+
actual fun logEvent(name: String, block: FirebaseAnalyticsParameters.() -> Unit) {
16+
val params = FirebaseAnalyticsParameters()
17+
params.block()
18+
logEvent(name, params.parameters)
19+
}
20+
actual fun setUserProperty(name: String, value: String) {
21+
android.setUserProperty(name, value)
22+
}
23+
actual fun setUserId(id: String) {
24+
android.setUserId(id)
25+
}
26+
actual fun resetAnalyticsData() {
27+
android.resetAnalyticsData()
28+
}
29+
actual fun setDefaultEventParameters(parameters: Map<String, String>) {
30+
android.setDefaultEventParameters(parameters.toBundle())
31+
}
32+
33+
actual fun setAnalyticsCollectionEnabled(enabled: Boolean) {
34+
android.setAnalyticsCollectionEnabled(enabled)
35+
}
36+
37+
actual fun setSessionTimeoutInterval(sessionTimeoutInterval: Long) {
38+
android.setSessionTimeoutDuration(sessionTimeoutInterval)
39+
}
40+
41+
actual suspend fun getSessionId(): Long? = android.sessionId.await()
42+
}
43+
44+
actual class FirebaseAnalyticsException(message: String): Exception(message)
45+
46+
fun Map<String, String>.toBundle() = Bundle().apply {
47+
forEach { (key, value) -> putString(key, value) }
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.gitlive.firebase.analytics
2+
3+
import org.junit.Ignore
4+
5+
actual val emulatorHost: String = "10.0.2.2"
6+
7+
actual val context: Any = ""
8+
9+
actual typealias IgnoreForAndroidUnitTest = Ignore
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dev.gitlive.firebase.analytics
2+
3+
import dev.gitlive.firebase.Firebase
4+
5+
expect val Firebase.analytics: FirebaseAnalytics
6+
7+
expect class FirebaseAnalytics {
8+
fun logEvent(name: String, parameters: Map<String, String>)
9+
fun logEvent(name: String, block: FirebaseAnalyticsParameters.() -> Unit)
10+
fun setUserProperty(name: String, value: String)
11+
fun setUserId(id: String)
12+
fun setAnalyticsCollectionEnabled(enabled: Boolean)
13+
fun setSessionTimeoutInterval(sessionTimeoutInterval: Long)
14+
suspend fun getSessionId(): Long?
15+
fun resetAnalyticsData()
16+
fun setDefaultEventParameters(parameters: Map<String, String>)
17+
}
18+
19+
expect class FirebaseAnalyticsException
20+
21+
data class FirebaseAnalyticsParameters(
22+
val parameters: MutableMap<String, String> = mutableMapOf()
23+
) {
24+
fun param(key: String, value: String) {
25+
parameters[key] = value
26+
}
27+
}
28+
29+
object FirebaseAnalyticsEvents {
30+
const val ADD_PAYMENT_INFO: String = "add_payment_info"
31+
const val ADD_SHIPPING_INFO: String = "add_shipping_info"
32+
const val ADD_TO_CART: String = "add_to_cart"
33+
const val ADD_TO_WISHLIST: String = "add_to_wishlist"
34+
const val AD_IMPRESSION: String = "ad_impression"
35+
const val APP_OPEN: String = "app_open"
36+
const val BEGIN_CHECKOUT: String = "begin_checkout"
37+
const val CAMPAIGN_DETAILS: String = "campaign_details"
38+
const val EARN_VIRTUAL_CURRENCY: String = "earn_virtual_currency"
39+
const val GENERATE_LEAD: String = "generate_lead"
40+
const val JOIN_GROUP: String = "join_group"
41+
const val LEVEL_END: String = "level_end"
42+
const val LEVEL_START: String = "level_start"
43+
const val LEVEL_UP: String = "level_up"
44+
const val LOGIN: String = "login"
45+
const val POST_SCORE: String = "post_score"
46+
const val PURCHASE: String = "purchase"
47+
const val REFUND: String = "refund"
48+
const val REMOVE_FROM_CART: String = "remove_from_cart"
49+
const val SCREEN_VIEW: String = "screen_view"
50+
const val SEARCH: String = "search"
51+
const val SELECT_CONTENT: String = "select_content"
52+
const val SELECT_ITEM: String = "select_item"
53+
const val SELECT_PROMOTION: String = "select_promotion"
54+
const val SHARE: String = "share"
55+
const val SIGN_UP: String = "sign_up"
56+
const val SPEND_VIRTUAL_CURRENCY: String = "spend_virtual_currency"
57+
const val TUTORIAL_BEGIN: String = "tutorial_begin"
58+
const val TUTORIAL_COMPLETE: String = "tutorial_complete"
59+
const val UNLOCK_ACHIEVEMENT: String = "unlock_achievement"
60+
const val VIEW_CART: String = "view_cart"
61+
const val VIEW_ITEM: String = "view_item"
62+
const val VIEW_ITEM_LIST: String = "view_item_list"
63+
const val VIEW_PROMOTION: String = "view_promotion"
64+
const val VIEW_SEARCH_RESULTS: String = "view_search_results"
65+
}

0 commit comments

Comments
 (0)