Skip to content

Commit

Permalink
Track click events (#1089)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashley Davies <[email protected]>
  • Loading branch information
ashdavies and ashdavies authored Jul 29, 2024
1 parent 01f7e71 commit 3c83f55
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 18 deletions.
1 change: 1 addition & 0 deletions after-party/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
kotlin {
sourceSets {
commonMain.dependencies {
implementation(projects.analytics)
implementation(projects.circuitSupport)
implementation(projects.composeMaterial)
implementation(projects.httpClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.screen.Screen
import io.ashdavies.analytics.OnClick
import io.ashdavies.paging.LazyPagingItems
import io.ashdavies.parcelable.Parcelable
import io.ashdavies.parcelable.Parcelize
Expand Down Expand Up @@ -71,9 +72,14 @@ internal fun EventsScreen(
FadeVisibility(state.pagingItems.itemCount > 0) {
LazyColumn(Modifier.fillMaxSize()) {
items(state.pagingItems.itemCount) {
val item = requireNotNull(state.pagingItems[it])

EventSection(
event = state.pagingItems[it],
event = item,
modifier = Modifier.animateItemPlacement(),
onClick = OnClick("event_item", mapOf("name" to item.name)) {
// Do nothing
},
)
}
}
Expand All @@ -85,6 +91,7 @@ internal fun EventsScreen(
EventSection(
event = null,
modifier = Modifier.animateItemPlacement(),
onClick = { },
)
}
}
Expand Down Expand Up @@ -117,10 +124,11 @@ private fun FadeVisibility(
private fun EventSection(
event: Event?,
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
Box(modifier.padding(horizontal = 16.dp, vertical = 8.dp)) {
Button(
onClick = { },
onClick = onClick,
modifier = Modifier.fillMaxWidth(),
enabled = event != null,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.slack.circuit.foundation.internal.BackHandler
import com.slack.circuit.runtime.CircuitUiEvent
import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.screen.Screen
import io.ashdavies.analytics.OnClick
import io.ashdavies.parcelable.Parcelable
import io.ashdavies.parcelable.Parcelize
import kotlinx.collections.immutable.ImmutableList
Expand Down Expand Up @@ -127,7 +128,9 @@ internal fun GalleryScreen(
Scaffold(
floatingActionButton = {
GalleryActionButton(
onClick = { eventSink(GalleryScreen.Event.Capture.Request) },
onClick = OnClick("gallery_capture") {
eventSink(GalleryScreen.Event.Capture.Request)
},
isActive = state.showCapture,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.material.icons.filled.Sync
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.ashdavies.analytics.OnClick
import io.ashdavies.material.BottomSheetItem

@Composable
Expand All @@ -18,14 +19,18 @@ internal fun GallerySheetContent(
Row(modifier) {
BottomSheetItem(
imageVector = Icons.Filled.Sync,
onClick = { eventSink(GalleryScreen.Event.Selection.Sync) },
onClick = OnClick("gallery_sync") {
eventSink(GalleryScreen.Event.Selection.Sync)
},
contentDescription = "Sync",
modifier = Modifier.padding(8.dp),
)

BottomSheetItem(
imageVector = Icons.Filled.Delete,
onClick = { eventSink(GalleryScreen.Event.Selection.Delete) },
onClick = OnClick("gallery_delete") {
eventSink(GalleryScreen.Event.Selection.Delete)
},
contentDescription = "Delete",
modifier = Modifier.padding(8.dp),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.slack.circuit.foundation.NavEvent
import com.slack.circuit.runtime.CircuitUiEvent
import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.screen.Screen
import io.ashdavies.analytics.OnClick
import io.ashdavies.events.EventsScreen
import io.ashdavies.gallery.GalleryScreen
import io.ashdavies.gallery.GallerySheetContent
Expand Down Expand Up @@ -68,7 +69,9 @@ internal fun AfterPartyScreen(
actions = {
ProfileActionButton(
identityState = state.identityState,
onClick = { eventSink(AfterPartyScreen.Event.Login) },
onClick = OnClick("profile_login") {
eventSink(AfterPartyScreen.Event.Login)
},
)
},
)
Expand Down
17 changes: 17 additions & 0 deletions analytics/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id("io.ashdavies.compose")
id("io.ashdavies.default")
}

android {
namespace = "io.ashdavies.analytics"
}

kotlin {
sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(libs.gitlive.firebase.analytics)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.ashdavies.analytics

import androidx.compose.runtime.Composable

internal typealias OnClickWith<P> = (P) -> Unit

internal typealias OnClick = () -> Unit

@Composable
public fun OnClick(
name: String,
parameters: Map<String, Any>? = null,
action: () -> Unit,
): OnClick {
val analytics = LocalAnalytics.current

return {
analytics.logEvent(name, parameters)
action()
}
}

@Composable
public fun <P> OnClickWith(
name: String,
parameters: Map<String, Any>? = null,
action: (P) -> Unit,
): OnClickWith<P> {
val analytics = LocalAnalytics.current

return {
analytics.logEvent(name, parameters)
action(it)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.ashdavies.analytics

import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.analytics.analytics

public val LocalAnalytics: ProvidableCompositionLocal<RemoteAnalytics> = staticCompositionLocalOf {
RemoteAnalytics { name, parameters -> Firebase.analytics.logEvent(name, parameters) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.ashdavies.analytics

public fun interface RemoteAnalytics {
public fun logEvent(name: String, parameters: Map<String, Any>?)
}
1 change: 1 addition & 0 deletions app-launcher/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ kotlin {

commonMain.dependencies {
implementation(projects.afterParty)
implementation(projects.analytics)
implementation(projects.appCheck.appCheckClient)
implementation(projects.circuitSupport)
implementation(projects.composeMaterial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import coil3.compose.rememberAsyncImagePainter
import com.slack.circuit.foundation.NavEvent
import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.screen.Screen
import io.ashdavies.analytics.OnClick
import io.ashdavies.parcelable.Parcelable
import io.ashdavies.parcelable.Parcelize
import org.jetbrains.compose.resources.DrawableResource
Expand All @@ -43,8 +44,8 @@ private val LauncherAspectRatio: Float
internal object LauncherScreen : Parcelable, Screen {

data class State(
val entries: List<Item> = emptyList(),
val eventSink: (NavEvent) -> Unit = { },
val entries: List<Item>,
val eventSink: (NavEvent) -> Unit,
) : CircuitUiState {

interface Item {
Expand Down Expand Up @@ -72,7 +73,9 @@ internal fun LauncherScreen(
LauncherItem(
item = entry,
modifier = modifier.padding(24.dp),
onClick = { eventSink(NavEvent.GoTo(entry.screen)) },
onClick = OnClick("launcher_goto", mapOf("screen" to entry.screen)) {
eventSink(NavEvent.GoTo(entry.screen))
},
)
}
}
Expand Down Expand Up @@ -106,7 +109,12 @@ private fun LauncherItem(
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
Card(modifier = modifier.clickable(onClick = onClick)) {
Card(
modifier = modifier.clickable(
onClickLabel = item.title,
onClick = onClick,
),
) {
Column {
val imagePainter = when (val imageModel = item.imageModel) {
is DrawableResource -> painterResource(imageModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public fun BottomSheetScaffold(
@Composable
public fun BottomSheetItem(
imageVector: ImageVector,
onClick: () -> Unit = { },
contentDescription: String? = null,
onClick: () -> Unit,
contentDescription: String?,
modifier: Modifier = Modifier,
) {
TextButton(
Expand Down
1 change: 1 addition & 0 deletions dominion-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ android {

kotlin {
sourceSets.commonMain.dependencies {
implementation(projects.analytics)
implementation(projects.circuitSupport)
implementation(projects.httpClient)
implementation(projects.pagingCompose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImagePainter
import coil3.compose.rememberAsyncImagePainter
import com.slack.circuit.runtime.Navigator
import io.ashdavies.analytics.OnClickWith
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList

Expand Down Expand Up @@ -89,7 +90,9 @@ internal fun BoxSetListScreen(
BoxSetListScreen(
boxSetList = state.boxSetList.toImmutableList(),
contentPadding = contentPadding,
onClick = { eventSink(DominionScreen.BoxSetList.Event.ShowBoxSet(it)) },
onClick = OnClickWith("show_box_set") {
eventSink(DominionScreen.BoxSetList.Event.ShowBoxSet(it))
},
)
}
}
Expand All @@ -100,8 +103,8 @@ private fun BoxSetListScreen(
boxSetList: ImmutableList<BoxSet>,
contentPadding: PaddingValues,
columnCount: Int = DEFAULT_COLUMN_COUNT,
onClick: (BoxSet) -> Unit = { },
modifier: Modifier = Modifier,
onClick: (BoxSet) -> Unit,
) {
LazyVerticalGrid(
columns = GridCells.Fixed(columnCount),
Expand All @@ -119,7 +122,7 @@ private fun BoxSetListScreen(
private fun BoxSetCard(
boxSet: BoxSet,
modifier: Modifier = Modifier,
onClick: () -> Unit = { },
onClick: () -> Unit,
) {
Box(
modifier = modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.unit.dp
import coil3.compose.rememberAsyncImagePainter
import com.slack.circuit.runtime.Navigator
import io.ashdavies.analytics.OnClickWith
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList

Expand Down Expand Up @@ -108,7 +109,9 @@ internal fun DetailsScreen(
DetailsScreen(
cards = state.cards.toImmutableList(),
contentPadding = contentPadding,
onClick = { eventSink(DominionScreen.BoxSetDetails.Event.ExpandCard(it)) },
onClick = OnClickWith("expand_card") {
eventSink(DominionScreen.BoxSetDetails.Event.ExpandCard(it))
},
)

AnimatedVisibility(
Expand All @@ -128,7 +131,7 @@ internal fun DetailsScreen(
@ExperimentalMaterial3Api
private fun DetailsTopBar(
title: String,
onBack: () -> Unit = { },
onBack: () -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
Surface(color = MaterialTheme.colorScheme.surface) {
Expand Down Expand Up @@ -166,7 +169,7 @@ private fun DetailsScreen(
cards: ImmutableList<Card>,
contentPadding: PaddingValues,
columnCount: Int = DEFAULT_COLUMN_COUNT,
onClick: (Card) -> Unit = { },
onClick: (Card) -> Unit,
modifier: Modifier = Modifier,
) {
LazyVerticalGrid(
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ coil-network = { module = "io.coil-kt.coil3:coil-network-ktor2", version.ref = "
fasterxml-jackson-core = "com.fasterxml.jackson.core:jackson-core:2.17.2"
fusesource-jansi = "org.fusesource.jansi:jansi:2.4.1"

gitlive-firebase-analytics = { module = "dev.gitlive:firebase-analytics", version.ref = "gitlive-firebase" }
gitlive-firebase-app = { module = "dev.gitlive:firebase-app", version.ref = "gitlive-firebase" }

google-accompanist-flowlayout = { module = "com.google.accompanist:accompanist-flowlayout", version.ref = "google-accompanist" }
Expand Down Expand Up @@ -102,6 +103,7 @@ mosaic-runtime = "com.jakewharton.mosaic:mosaic-runtime:0.13.0"
pinterest-ktlint-bom = "com.pinterest.ktlint:ktlint-bom:1.3.1"

slack-circuit-foundation = { module = "com.slack.circuit:circuit-foundation", version.ref = "slack-circuit" }
slack-circuit-runtime = { module = "com.slack.circuit:circuit-runtime", version.ref = "slack-circuit" }
slack-circuit-overlay = { module = "com.slack.circuit:circuit-overlay", version.ref = "slack-circuit" }
slack-circuit-test = { module = "com.slack.circuit:circuit-test", version.ref = "slack-circuit" }

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ develocity.buildScan {
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

include(
":analytics",
":after-party",
":app-check:app-check-client",
":app-check:app-check-common",
Expand Down

0 comments on commit 3c83f55

Please sign in to comment.