Skip to main content

Usage Stats Manager Capacitor plugin

Access Android usage statistics to track app usage time and screen time analytics

Install

bun add @capgo/capacitor-android-usagestatsmanager bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-android-usagestatsmanager
  • pnpm add @capgo/capacitor-android-usagestatsmanager
  • yarn add @capgo/capacitor-android-usagestatsmanager

Guide

How to use Usage Stats Manager in Capacitor

Using @capgo/capacitor-android-usagestatsmanager

Capacitor plugin for accessing Android UsageStatsManager API.

Install

bun add @capgo/capacitor-android-usagestatsmanager
bunx cap sync

What This Plugin Exposes

  • queryAndAggregateUsageStats - Queries and aggregates usage stats for the given time range.
  • isUsageStatsPermissionGranted - Checks if the usage stats permission is granted.
  • openUsageStatsSettings - Open the usage stats settings screen. This will open the usage stats settings screen, which allows the user to grant the usage stats permission. This will always open the settings screen, even if the permission is already granted.
  • queryAllPackages - Queries all installed packages on the device. Requires the QUERY_ALL_PACKAGES permission.

Example Usage

queryAndAggregateUsageStats

Queries and aggregates usage stats for the given time range.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;
const now = Date.now();
const stats = await CapacitorUsageStatsManager.queryAndAggregateUsageStats({
  beginTime: oneDayAgo,
  endTime: now
});

for (const [packageName, usageData] of Object.entries(stats)) {
  console.log(`${packageName}: ${usageData.totalTimeInForeground}ms`);
}

isUsageStatsPermissionGranted

Checks if the usage stats permission is granted.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

const { granted } = await CapacitorUsageStatsManager.isUsageStatsPermissionGranted();
if (!granted) {
  await CapacitorUsageStatsManager.openUsageStatsSettings();
}

openUsageStatsSettings

Open the usage stats settings screen. This will open the usage stats settings screen, which allows the user to grant the usage stats permission. This will always open the settings screen, even if the permission is already granted.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

await CapacitorUsageStatsManager.openUsageStatsSettings();

queryAllPackages

Queries all installed packages on the device. Requires the QUERY_ALL_PACKAGES permission.

import { CapacitorUsageStatsManager } from '@capgo/capacitor-android-usagestatsmanager';

const { packages } = await CapacitorUsageStatsManager.queryAllPackages();
packages.forEach(pkg => {
  console.log(`${pkg.appName} (${pkg.packageName}): v${pkg.versionName}`);
});

Full Reference

Keep going from Using @capgo/capacitor-android-usagestatsmanager

If you are using Using @capgo/capacitor-android-usagestatsmanager to plan dashboard and API operations, connect it with @capgo/capacitor-android-usagestatsmanager for the implementation detail in @capgo/capacitor-android-usagestatsmanager, Getting Started for the implementation detail in Getting Started, API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, and API Keys for the implementation detail in API Keys.

FAQ

Usage Stats Manager plugin FAQ

How do I install the Usage Stats Manager plugin in a Capacitor app?

Run "bun add @capgo/capacitor-android-usagestatsmanager" (or "npm install @capgo/capacitor-android-usagestatsmanager"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-android-usagestatsmanager" in your app code.

Does @capgo/capacitor-android-usagestatsmanager work with React, Vue and Angular?

Yes. Usage Stats Manager is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-android-usagestatsmanager support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-android-usagestatsmanager free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-android-usagestatsmanager/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Usage Stats Manager without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Usage Stats Manager changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo