|
| 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 | +} |
0 commit comments