Get detailed analytics and statistics from your Github Actions
Tags: github, javascript, git, actionshackathon21
Published: 2021-12-08 06:25:20

I love Github Actions, love building them, love using them and love helping others use them. But that last part is a bit difficult as Github does not provide a way to get analytical data on your action's usage by others such as: Who uses it? How often are they using it? Are there any errors when people try to use it? Do people try to use it and then decide not to due to some problems? Etc…

By getting the answers to these questions, Actions developers would be able to improve their actions to suit the needs of more people and make the experience for users better. So we decided to solve this problem and give developers the insights they need.

---
**Getting Started**
Start by creating you action, we'll be using the code from Github's tutorial on creating a new JS action you can find [here](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action).

```javascript
const core = require('@actions/core');
const github = require('@actions/github');

try {
  // `who-to-greet` input defined in action metadata file
  const nameToGreet = core.getInput('who-to-greet');
  console.log(`Hello ${nameToGreet}!`);
  const time = (new Date()).toTimeString();
  core.setOutput("time", time);
  // Get the JSON webhook payload for the event that triggered the workflow
  const payload = JSON.stringify(github.context.payload, undefined, 2)
  console.log(`The event payload: ${payload}`);
} catch (error) {
  core.setFailed(error.message);
}
```
To start collecting data we install the `gh-action-stats` npm package:

```bash
npm install -S gh-action-stats
```
And then import it into our action and call the collectStats function as such:
```javascript
const core = require('@actions/core');
const github = require('@actions/github');
const collectStats = require('gh-action-stats');

try {
  collectStats(); // You can collect stats at any point in the action.
  // `who-to-greet` input defined in action metadata file
  const nameToGreet = core.getInput('who-to-greet');
  console.log(`Hello ${nameToGreet}!`);
  const time = (new Date()).toTimeString();
  core.setOutput("time", time);
  // Get the JSON webhook payload for the event that triggered the workflow
  const payload = JSON.stringify(github.context.payload, undefined, 2)
  console.log(`The event payload: ${payload}`);
} catch (error) {
  core.setFailed(error.message);
}
```
By doing this you will start collecting statistics on your action once it is run in the context of a runner (does not currently support self hosted runners).

**Collecting Run Statistics**
The configuration above will work well but will leave out two key pieces of information: Errors and Duration of the run (how long did your action take to run).
To get these information we need to call the `collectStats` function differently:
```javascript
const core = require('@actions/core');
const github = require('@actions/github');
const collectStats = require('gh-action-stats');

function main() {
  try {
    // `who-to-greet` input defined in action metadata file
    const nameToGreet = core.getInput('who-to-greet');
    console.log(`Hello ${nameToGreet}!`);
    const time = (new Date()).toTimeString();
    core.setOutput("time", time);
    // Get the JSON webhook payload for the event that triggered the workflow
    const payload = JSON.stringify(github.context.payload, undefined, 2)
    console.log(`The event payload: ${payload}`);
  } catch (error) {
    core.setFailed(error.message);
    // Rethrow the error so that it can be caught and logged 
    // by gh-action-stats 
    throw error; 
  }
}

collectStats(main);
```
By creating a main function, and passing it to the `collectStats` function, gh-action-stats will be able to execute your main function (even if it is `async`), collect all the previous statistics, catch errors and log them, and monitor run duration!

**Accessing Data**
Once you've configured your action to collect statistics, you will want to access your data. Go to [actions.boringday.co](https://actions.boringday.co/) and log-in using your Github account. Note, you will only be able to see the statistics for the actions you own (i.e. which you are the creator of). Once logged in and some run data has been collected you will have your dashboard with the list of the actions you own and the last time they were used.

![List of all your actions which currently have data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atm9r1orhcid2lt4rf96.png)

Select the action you wish to visualize and the following dashboard will appear giving you insights on your action.

![Dashboard for my version-bumper actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bcw8zbzn072416c7mj8h.png)

**Notes**
For the privacy of those using the actions, you will not be able to see the name of the repository if it is private to you, you will still be able to see the repo owner's name though if you want to contact them.
You can find the repository for the platform here: [https://github.com/michmich112/gh-action-stats ](https://github.com/michmich112/gh-action-stats)
And the npm package here: [https://github.com/michmich112/gh-action-stats-js ](https://github.com/michmich112/gh-action-stats-js)
You can visit our feature plans there and contributions are welcome!
This currently only works for JS Actions only, support for Docker images is on the way.