Skip to content

Commit 5f1fd02

Browse files
authored
add widgets metrics api snippet (#641)
* add widgets metrics api snippet
1 parent c4096d2 commit 5f1fd02

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

compose/snippets/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ plugins {
2525
}
2626

2727
android {
28-
compileSdk = libs.versions.compileSdk.get().toInt()
28+
compileSdk {
29+
version = release(libs.versions.compileSdk.get().toInt())
30+
{minorApiLevel = 1}} // Android 16 QPR 2
2931
namespace = "com.example.compose.snippets"
3032

3133
defaultConfig {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.glance
18+
19+
import android.appwidget.AppWidgetManager
20+
import android.content.Context
21+
import android.os.Build
22+
import android.util.Log
23+
import androidx.annotation.RequiresApi
24+
25+
private const val TAG = "WidgetMetrics"
26+
27+
class GlanceMetrics {
28+
29+
// [START android_compose_glance_metrics]
30+
@RequiresApi(Build.VERSION_CODES_FULL.BAKLAVA_1)
31+
fun getWidgetEngagementMetrics(context: Context) {
32+
val manager = AppWidgetManager.getInstance(context)
33+
34+
val endTime = System.currentTimeMillis()
35+
val startTime = endTime - (24 * 60 * 60 * 1000) // a day ago
36+
37+
val events = manager.queryAppWidgetEvents(startTime, endTime)
38+
39+
if (events.isEmpty()) {
40+
Log.d(TAG, "No events found for the given time range.")
41+
}
42+
43+
val metrics = hashMapOf(
44+
"clicks" to 0L,
45+
"scrolls" to 0L,
46+
"totalImpressionLength" to 0L
47+
)
48+
49+
for (event in events) {
50+
51+
Log.d(TAG, "Event Start: ${event.start}")
52+
Log.d(TAG, "Event End: ${event.end}")
53+
54+
val widgetId = event.appWidgetId
55+
56+
// Tap actions
57+
val clickedIds = event.clickedIds
58+
if (clickedIds?.isNotEmpty() == true) {
59+
metrics["clicks"] = metrics.getValue("clicks") + clickedIds.size
60+
// Log or analyze which components were clicked.
61+
for (id in clickedIds) {
62+
Log.d(TAG, "Widget $widgetId: Tap event on component with ID $id")
63+
}
64+
}
65+
66+
// Scroll events
67+
val scrolledIds = event.scrolledIds
68+
if (scrolledIds?.isNotEmpty() == true) {
69+
metrics["scrolls"] = metrics.getValue("scrolls") + scrolledIds.size
70+
// Log or analyze which lists were scrolled.
71+
for (id in scrolledIds) {
72+
Log.d(TAG, "Widget $widgetId: Scroll event in list with ID/tag $id")
73+
}
74+
}
75+
76+
// Impressions
77+
metrics["totalImpressionLength"] = metrics.getValue("totalImpressionLength") + event.visibleDuration.toMillis()
78+
Log.d(
79+
TAG,
80+
"Widget $widgetId: Impression event with duration " + event.visibleDuration.toMillis() + "ms"
81+
)
82+
83+
// Position
84+
val position = event.position
85+
if (position != null) {
86+
Log.d(
87+
TAG,
88+
"Widget $widgetId: left=${position.left}, right=${position.right}, top=${position.top}, bottom=${position.bottom}"
89+
)
90+
}
91+
}
92+
Log.d("WidgetMetrics", "Metrics: $metrics")
93+
}
94+
// [END android_compose_glance_metrics]
95+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ androidxHiltNavigationCompose = "1.3.0"
3535
appcompat = "1.7.1"
3636
coil = "2.7.0"
3737
# @keep
38-
compileSdk = "36"
38+
compileSdk = "36" # gradle.kts for Module :compose:snippets uses Android 16 QPR 2
3939
compose-latest = "1.9.2"
4040
composeUiTooling = "1.5.2"
4141
coreSplashscreen = "1.0.1"

0 commit comments

Comments
 (0)