Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active July 7, 2023 20:23
Show Gist options
  • Save joshdholtz/8ee79bc2c617826f220aeff7a447e126 to your computer and use it in GitHub Desktop.
Save joshdholtz/8ee79bc2c617826f220aeff7a447e126 to your computer and use it in GitHub Desktop.
Update LaMetric with RevenueCat Developer API and GitHub Actions

Show RevenueCat Overview Metrics on LaMetrics

  • User RevenueCat's new Developer API (beta)
  • Runs cron job on GitHub Actions every 30 minutes to update data.json
  • LaMetric app polls data.json file every hour

It shows:

  • Total Revenue
  • MRR
  • Active Subscriptions
  • Active Users
  • New Customers

It will total up all of these over all of the configured projects

Note: this demo is only using one of my sample apps

IMG_9165.mp4
source "https://rubygems.org"
gem "json"
# .github/workflows/main.yml
name: Update
permissions:
contents: write
on:
workflow_dispatch:
schedule:
- cron: '*/30 * * * *'
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
with:
ruby-version: '3.1'
- name: Install dependencies
run: bundle install
- name: Run updates
run: bundle exec ruby update.rb
- name: Commit files
run: |
git config --local user.email "[email protected]"
git config --local user.name "github-actions[bot]"
git commit -a -m "Add changes"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
def self.get_overview_metrics(api_key:, project_id:)
url = URI("https://api.revenuecat.com/v2/projects/#{project_id}/metrics/overview")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Authorization"] = "Bearer #{api_key}"
response = http.request(request)
json = JSON.parse(response.read_body)
return json
end
# This should probably be a github secret
projects = {
"projXXXXXXX": "sk_XXXXXXXXXXXXXX"
}
revenue = 0
mrr = 0
subs = 0
active = 0
new_cust = 0
projects.each do |project_id, api_key|
resp = get_overview_metrics(api_key: api_key, project_id: project_id)
metrics = resp["metrics"]
metrics.each do |metric|
case metric["id"]
when "revenue"
revenue += metric["value"]
when "mrr"
mrr += metric["value"]
when "active_subscriptions"
subs += metric["value"]
when "active_users"
active += metric["value"]
when "new_customers"
new_cust += metric["value"]
end
end
end
data = {
frames: [
{
text: "$#{revenue.to_s}",
icon: 34
},
{
text: "$#{mrr.to_s}",
icon: 8381
},
{
text: subs.to_s,
icon: 42832
},
{
text: active.to_s,
icon: 5337
},
{
text: new_cust.to_s,
icon: 14024
},
]
}
File.write('data.json', data.to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment