Skip to content

ilovelinux/ask-metrics-v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASK Metrics v2

Warning

This is a beta release of the ASK Metrics v2 API. The API and documentation are subject to change.

Overview

ASK Metrics v2 allows you to fetch metrics from your Alexa skill. You can use these metrics to build your own custom analytics dashboards or to integrate with your existing analytics systems.

ASK Api reference: https://developer.amazon.com/en-US/docs/alexa/smapi/metrics-api.html

Getting Started

Installation

npm install --save ask-metrics-v2

Usage

For refresh token, see ASK Smapi SDK Documentation

Edxample

// test.ts
import {
  RefreshTokenConfig,
  createClient,
  AggregationPeriod,
  Metrics,
  MetricsV2RequestOptions,
  ListMetricsV2RequestOptions,
} from "ask-metrics-v2";

// Skill ID to fetch metrics for
const skillId = 'amzn1.ask.skill.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

// Refresh Token to authenticate with
// See ASK Smapi SDK documentation for more information
const refreshTokenConfig: RefreshTokenConfig = {
  clientId
  clientSecter,
  refreshToken,
};

void main() {
  // Setup client
  const client = createClient(refreshTokenConfig);

  // Create request options
  const requestListOptions: ListMetricsV2RequestOptions = {
    metricNamespace: Metrics.Custom.namespace,
  };

  // Get list metrics
  const listMetrics = await client.getListMetricsV2(requestListOptions);

  // Print list metrics
  console.log('List Metrics:');
  console.log(JSON.stringify(listMetrics, null, 2));

  // Create request options
  const requestOptions: MetricsV2RequestOptions = {
    startTime: Date.now()-1000*60*60*24*30, // 30 days before
    endTime: Date.now(),
    metricQueries: [
      {
        id: "m2",
        name: Metrics.Custom.CUSTOMERS,
        metricNamespace: Metrics.Custom.namespace,
        aggregationPeriod: AggregationPeriod.BY_DAY,
      }
    ]
  };

  // Get metrics
  const metrics = await client.getSkillMetricsV2(skillId, requestOptions);

  // Print metrics
  console.log("Metrics:");
  console.log(JSON.stringify(metrics, null, 2));
}

main();