Skip to content

Instantly share code, notes, and snippets.

@daolf
Last active March 7, 2023 14:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daolf/ae104b1ab7cabf564b47770c88d4214b to your computer and use it in GitHub Desktop.
Save daolf/ae104b1ab7cabf564b47770c88d4214b to your computer and use it in GitHub Desktop.
MRR iOS widget
// Modified from @mutsuda's https://medium.com/@mutsuda/create-an-ios-widget-showing-google-spreadsheets-data-856767a9447e
// Modified from @levelsio's https://gist.github.com/levelsio/a1ca0dd434b77ef26f6a7c403beff4d0
// By @daolf
// Prerequisite: install Scriptable @ https://apps.apple.com/us/app/scriptable/id1405459188
const API_TOKEN = "CHARTMOGUL_TOKEN"
const API_SECRET = "CHARTMOGUL_SECRET"
// Recreating a basic auth with Scriptable lib
const auth = "Basic " + btoa(`${API_TOKEN}:${API_SECRET}`);
const currentYear = new Date().getYear();
const startDate = `${currentYear}-01-01`
const endDate = `${currentYear}-12-31`
const endpoint = `https://api.chartmogul.com/v1/metrics/mrr?start-date=${startDate}&end-date=${endDate}`
async function loadItems() {
let at = endpoint
let req = new Request(at)
req.headers = {"Authorization" : auth}
let response = await req.loadJSON()
return response
}
// Request the MRR data
let json = await loadItems()
MRR = Math.floor(json["summary"]["current"] / 100).toString()
// Create the widget
let w = new ListWidget()
w.backgroundColor = new Color("#000000")
// Formating number i.e 19000 -> $19,000
t = w.addText(`\$${new Intl.NumberFormat('en-US').format(MRR)}`)
t.textColor = Color.white()
t.font = new Font("Avenir-Heavy",24)
Script.setWidget(w)
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment