Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Implement traking (#95)
Browse files Browse the repository at this point in the history
* Implement firebase traking

* fix detetk issue
  • Loading branch information
VovaStelmashchuk authored Oct 12, 2023
1 parent e7b5e1b commit 182933a
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 68 deletions.
38 changes: 22 additions & 16 deletions androidApp/src/androidMain/kotlin/org/mixdrinks/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
Expand All @@ -27,12 +28,13 @@ import com.google.firebase.ktx.Firebase
import setAppleAuthStart
import setGoogleAuthStart
import setLogout
import trackAnalyticsCallback


@Keep
class MainActivity : AppCompatActivity() {

lateinit var googleSignInClient: GoogleSignInClient
private lateinit var googleSignInClient: GoogleSignInClient
private lateinit var register: ActivityResultLauncher<Intent>
private lateinit var firebaseAuth: FirebaseAuth

Expand All @@ -42,6 +44,18 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

trackAnalyticsCallback = { action, data ->
FirebaseAnalytics.getInstance(applicationContext)
.logEvent(
action,
Bundle().apply {
data.forEach { (key, value) ->
putString(key, value)
}
})
}

val deepLink = intent?.data?.toString()
window.statusBarColor = android.graphics.Color.parseColor("#FF2B4718")
setContent {
Expand Down Expand Up @@ -75,15 +89,7 @@ class MainActivity : AppCompatActivity() {

override fun onStart() {
super.onStart()
firebaseAuth.currentUser?.getIdToken(true)
?.addOnCompleteListener {
it.result?.token?.let { token ->
NewToken(token)
}
}
?.addOnFailureListener {
Firebase.crashlytics.recordException(it)
}
firebaseAuth.currentUser?.sendNewToken()
register = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(result.data)
Expand All @@ -97,14 +103,14 @@ class MainActivity : AppCompatActivity() {
val pending = firebaseAuth.pendingAuthResult
if (pending != null) {
pending.addOnSuccessListener { authResult ->
authResult.user.sendNewToken(AuthProvider.APPLE)
authResult.user.sendNewToken()
}.addOnFailureListener { e ->
authFail(AuthProvider.APPLE, e)
}
} else {
firebaseAuth.startActivityForSignInWithProvider(this, appleProvider.build())
.addOnSuccessListener { authResult ->
authResult.user.sendNewToken(AuthProvider.APPLE)
authResult.user.sendNewToken()
}
.addOnFailureListener { e ->
authFail(AuthProvider.APPLE, e)
Expand Down Expand Up @@ -133,16 +139,16 @@ class MainActivity : AppCompatActivity() {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { _ ->
FirebaseAuth.getInstance().currentUser?.sendNewToken(AuthProvider.GOOGLE)
FirebaseAuth.getInstance().currentUser?.sendNewToken()
}
.addOnFailureListener { e ->
authFail(AuthProvider.GOOGLE, e)
}
}

private fun FirebaseUser?.sendNewToken(provider: AuthProvider) {
private fun FirebaseUser?.sendNewToken() {
if (this == null) {
authFail(provider, Exception("User is null"))
Firebase.crashlytics.recordException(Exception("User is null"))
return
}

Expand All @@ -153,7 +159,7 @@ class MainActivity : AppCompatActivity() {
}
}
.addOnFailureListener {
authFail(provider, it)
Firebase.crashlytics.recordException(it)
}
}

Expand Down
8 changes: 7 additions & 1 deletion iosApp/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use_frameworks!

platform :ios, '15.0'

install! 'cocoapods', :deterministic_uuids => false

target 'iosApp' do
use_frameworks!
platform :ios, '14.1'
Expand All @@ -7,4 +13,4 @@ end
pod 'GoogleSignIn'
pod 'FirebaseCore'
pod 'FirebaseAuth'
pod 'FirebaseAnalytics'
pod 'FirebaseAnalytics'
32 changes: 16 additions & 16 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
7555FF79242A565900829871 /* Resources */,
F85CB1118929364A9C6EFABC /* Frameworks */,
2134C13603D0B299603D9F49 /* [CP] Copy Pods Resources */,
A53E69582B14720372B84606 /* [CP] Embed Pods Frameworks */,
558627EC635BACAE0E100860 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -213,43 +213,43 @@
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources.sh\"\n";
showEnvVarsInLog = 0;
};
98D614C51D2DA07C614CC46E /* [CP] Check Pods Manifest.lock */ = {
558627EC635BACAE0E100860 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-iosApp-checkManifestLockResult.txt",
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
A53E69582B14720372B84606 /* [CP] Embed Pods Frameworks */ = {
98D614C51D2DA07C614CC46E /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-iosApp-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-frameworks.sh\"\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
Expand Down
5 changes: 3 additions & 2 deletions iosApp/iosApp/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@ class MainViewModel: NSObject, ObservableObject, ASAuthorizationControllerDelega

GIDSignIn.sharedInstance.signIn(withPresenting: getUiWindow().rootViewController!) { [unowned self] result, error in
guard error == nil else {
fatalError("Error is null")
print("error google \(error)")
return
}

guard let user = result?.user,
let idToken = user.idToken?.tokenString
else {
fatalError("resul?.User is null")
return
}

let credential = GoogleAuthProvider.credential(withIDToken: idToken,
Expand Down
18 changes: 16 additions & 2 deletions iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance.handle(url)
var handled: Bool

handled = GIDSignIn.sharedInstance.handle(url)
if handled {
return true
}
// Handle other custom URL types.
return false
}

}
Expand All @@ -26,7 +33,14 @@ struct iOSApp: App {

var body: some Scene {
WindowGroup {
ContentView()
ZStack {
GeometryReader { reader in
Color.black
.frame(height: reader.safeAreaInsets.top, alignment: .top)
.ignoresSafeArea()
}
ContentView()
}
}
}
}
9 changes: 9 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ kotlin {
}
extraSpecAttributes["resources"] =
"['src/commonMain/resources/**', 'src/iosMain/resources/**']"
podfile = project.file("../iosApp/Podfile")

pod("GoogleSignIn")
pod("FirebaseCore")
pod("FirebaseAuth")
pod("FirebaseAnalytics")
}

sourceSets {
Expand Down Expand Up @@ -73,6 +79,8 @@ kotlin {
api("androidx.activity:activity-compose:1.7.2")
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.10.1")
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
implementation("com.google.firebase:firebase-analytics-ktx")
}
}
val iosX64Main by getting
Expand Down Expand Up @@ -109,6 +117,7 @@ android {
}

dependencies {
implementation("com.google.firebase:firebase-common-ktx:20.4.0")
add("kspCommonMainMetadata", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion")
add("kspAndroid", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion")
add("kspIosX64", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion")
Expand Down
2 changes: 0 additions & 2 deletions shared/src/androidMain/AndroidManifest.xml

This file was deleted.

3 changes: 0 additions & 3 deletions shared/src/androidMain/kotlin/main.android.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.decompose.defaultComponentContext
import com.arkivanov.essenty.backhandler.BackDispatcher
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import org.mixdrinks.app.MixDrinksApp
import org.mixdrinks.di.GraphHolder
import org.mixdrinks.ui.auth.AuthCallbacks
Expand Down
4 changes: 4 additions & 0 deletions shared/src/androidMain/kotlin/trackEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var trackAnalyticsCallback: (action: String, data: Map<String, String>) -> Unit = { _, _ -> }
actual fun trackEvent(action: String, data: Map<String, String>) {
trackAnalyticsCallback(action, data)
}
17 changes: 17 additions & 0 deletions shared/src/commonMain/kotlin/org/mixdrinks/data/Tracking.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mixdrinks.data

import trackEvent

object Tracking {

fun track(
action: String,
screen: String,
data: Map<String, String> = emptyMap(),
) {
val mapToSend = data.toMutableMap()
mapToSend["screen"] = screen

trackEvent(action, mapToSend)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ internal fun ItemViewContent(
)
}
cocktailListInserter(
cocktails,
predefineComponent::navigateToDetails,
predefineComponent::navigateToTagCocktails
cocktails = cocktails,
onClick = predefineComponent::navigateToDetails,
onTagClick = predefineComponent::navigateToTagCocktails,
trackingScreen = "page_item_details"
)
}
}
Expand Down
Loading

0 comments on commit 182933a

Please sign in to comment.