npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alexa-content-analytics

v1.0.22

Published

Enables a custom skill to integrate with a content analytics service.

Downloads

10

Readme

Alexa content analytics package adds content analytics to a custom skill by emitting events to a designated endpoint. It currently supports http API endpoints (see sample AWS serverless http api endpoint)

Install

First install the npm package in your project.


npm install alexa-content-analytics 

Response interceptor

Call AlexaContentAnalytics client in the custom skill's response interceptor so that every invocation is captured. If this is the first time you are adding a response interceptor to your custom skill make sure to add the interceptor in your lambda handler.


// requestInterceptor

const AlexaContentAnalytics  = require('alexa-content-analytics');

AlexaContentAnalytics.capture(handlerInput, config, payload);

Sample skill response interceptor


// interceptors/ResponseInterceptors
const AlexaContentAnalytics  = require('alexa-content-analytics');

export const ResponseInterceptor = {

    async process(handlerInput: any) {

        try{
            let payload = {}; // you can pass empty object or add more attributes to capture for example the http stream track title or player state
            let config = { // analytics endpoint configuration
                    endPointUrl: "https://your-analytics-endpoint"
                    headers: {
                            {"your receiving analytics endpoint auth http headers"}
                        }
                }
            AlexaContentAnalytics.capture(handlerInput, config, payload);
        } catch (e) {
            console.error(JSON.stringify(e.message));
        }
    }
}

Request Interceptor (optional)

If you wish to capture start time in seconds (so that you can work out total request duration in your analytics), you need to add the following code in your skill's request interceptor.

// sample skill request interceptor
// interceptors/requestInterceptor

import * as Alexa from 'ask-sdk';
const { DateTime } = require("luxon");
export const RequestInterceptor = {

    async process(handlerInput: any) {
       
        try {
            let alexaContentAnalyticsRequestStartSecs = '';
            alexaContentAnalyticsRequestStartSecs = DateTime.now().toUnixInteger();
            let requestAttributes = await handlerInput.attributesManager.getRequestAttributes();
            requestAttributes.alexaContentAnalyticsRequestStartSecs = alexaContentAnalyticsRequestStartSecs;
            await handlerInput.attributesManager.setRequestAttributes(requestAttributes);
        } catch (e: any) {
            console.error("RequestInterceptor", JSON.stringify(e.message));
        }
    }

}

Add interceptor(s) to your lambda handler


export const handler = Alexa.SkillBuilders.custom()
    // call request interceptor before anything else 
    .addRequestInterceptors(RequestInterceptor) 

    // skill's other handlers here

    // call response interceptor last
    .addResponseInterceptors(ResponseInterceptor) 
    .lambda();

Client Parameters

handlerInput

The handlerInput object passed to your skill on invocation.

config

Configuration for the Alexa Analytics Client.

endPointUrl (String). The https endpoint that will receive the events sent from your skill.

headers (Object) Authorization headers for the receiving endpoint.


{
    endpointType: "http", // htttp sends event to http API,  ga for google analytics
    endPointUrl: "https://your-analytics-endpoint", // required when endpointType is http 
    ga_measurement_id: 'G-xxxxxxxxx', //required if sending to google analytics
    ga_api_secret: '`your GA4 data stream api key',
    headers: {
      your auth http headers
    },
    captureFullEnvelope: false,
}

For more information on response interceptors see the Alexa documentation on interceptors.

payload (Object). A json object with any data you want to capture in addition to what is captured automatically. For example the particular piece of content served to the user like stream title, news piece, quiz etc.


// Alexa-content-analytics client will capture the following information when invoked.

 // request start point, please enable in the response interceptor of your skill
 payload.startRequestSecs

// size of persistent storage used for the particular user
payload.persistentStorage.sizeUnit  // size storage unit (b, kb, mb etc)
  
// returns true if the device has a screen 
payload.displaydevicesupport 

// the resolution of the device if available 
payload.displayresolution 

// supported device interfaces for the device, for example APL
payload.devicesupportinterfaces

// unix seconds for the request endtime. 
// Can be used with startRequestSecs (if enabled to calculate total duration)
payload.endRequestSecs 

// intent invoked (if applicable)
payload.intentName 

// request type (for example playBackStop)
payload.requestType 

// the id of the device
payload.deviceId  

// device locale, for example en_au
payload.locale  

// the skill id invoked
payload.applicationId  

// the full request envelope 
payload.requestEnvelopeRequest 

Authorization modes

At present the client supports https API endpoints (PUT http method), utilzing HTTP headers for authorization. For more info on setting up an http endpoint see sample serverless http API for more.

Output

The client emits a json object.