Skip to content

Instantly share code, notes, and snippets.

@neopheus
Forked from simon-hv/uber-eats.js
Created December 8, 2023 09:11
Show Gist options
  • Save neopheus/3cc521ed9a299c99a33959192592664a to your computer and use it in GitHub Desktop.
Save neopheus/3cc521ed9a299c99a33959192592664a to your computer and use it in GitHub Desktop.
Uber Eats Stats and Analytics - See your total spend, the restaurant you've ordered from the most, and your total spend by year
/**
* This script will fetch all your orders from UberEats and display some stats
*
* How to use:
* Go to https://www.ubereats.com/ and sign in
* Open the console of your browser: F12 key or right click anywhere on the page and click "Inspect"
* Go to the "Console" tab
* Copy/paste the code below
* Press enter
*/
const allOrders = []
let currency
const fetchAllOrders = async (lastWorkflowUUID = '') => {
const res = await fetch('/api/getPastOrdersV1?localeCode=fr', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' },
credentials: 'include',
body: JSON.stringify({ lastWorkflowUUID }),
})
const { data } = await res.json()
allOrders.push(...Object.values(data.ordersMap))
console.log(
`Fetching data, please wait...\nTotal orders fetched: ${allOrders.length}`
)
if (data.meta?.hasMore) {
const lastOrder = allOrders[allOrders.length - 1]
return fetchAllOrders(lastOrder.baseEaterOrder?.uuid)
} else {
return true
}
}
const computeTotal = orders => {
return orders.reduce((acc, order) => {
const { fareInfo } = order
return acc + fareInfo?.totalPrice
}, 0)
}
const getMostOrderedRestaurant = () => {
const restaurants = allOrders.reduce((acc, order) => {
const { storeInfo } = order
if (acc[storeInfo.uuid]) {
acc[storeInfo.uuid]++
} else {
acc[storeInfo.uuid] = 1
}
return acc
}, {})
const mostOrderedRestaurant = Object.keys(restaurants).reduce((a, b) =>
restaurants[a] > restaurants[b] ? a : b
)
const allOrdersFromRestaurant = allOrders.filter(
order => order.storeInfo.uuid === mostOrderedRestaurant
)
return {
name: allOrdersFromRestaurant[0]?.storeInfo?.title,
count: restaurants[mostOrderedRestaurant],
total: Math.round(computeTotal(allOrdersFromRestaurant)) / 100,
}
}
const getMostExpensiveRestaurant = () => {
const restaurants = allOrders.reduce((acc, order) => {
const { storeInfo } = order
if (acc[storeInfo.uuid]) {
acc[storeInfo.uuid] += order.fareInfo?.totalPrice
} else {
acc[storeInfo.uuid] = order.fareInfo?.totalPrice
}
return acc
}, {})
const mostExpensiveRestaurant = Object.keys(restaurants).reduce((a, b) =>
restaurants[a] > restaurants[b] ? a : b
)
const restaurantName = allOrders.find(
order => order.storeInfo.uuid === mostExpensiveRestaurant
)?.storeInfo?.title
return {
name: restaurantName,
total: Math.round(restaurants[mostExpensiveRestaurant]) / 100,
count: allOrders.filter(
order => order.storeInfo.uuid === mostExpensiveRestaurant
).length,
}
}
const displayTotal = totalPrice => {
console.log(
`%cYou have ${allOrders.length} orders for a total of ${totalPrice} ${currency}`,
'color: cyan; font-size: 20px'
)
}
const displayMostOrderedRestaurant = ({ name, count, total }) => {
console.log(
`%cThe most ordered restaurant is ${name}! You ordered there ${count} times for a total of ${total} ${currency}`,
'color: green; font-size: 20px'
)
}
const diplayMostExpensiveRestaurant = ({ name, count, total }) => {
console.log(
`%cThe most expensive restaurant is ${name}! You ordered there ${count} times for a total of ${total} ${currency}`,
'color: red; font-size: 18px'
)
}
const displayTotalPricesPerYear = () => {
const pricesPerYear = allOrders.reduce((acc, order) => {
const { fareInfo } = order
const year = new Date(order.baseEaterOrder?.lastStateChangeAt).getFullYear()
if (acc[year]) {
acc[year] += fareInfo?.totalPrice
} else {
acc[year] = fareInfo?.totalPrice
}
return acc
}, {})
const pricesPerYearRounded = Object.keys(pricesPerYear).reduce(
(acc, year) => {
acc[year] = `${Math.round(pricesPerYear[year]) / 100} ${currency}`
return acc
},
{}
)
console.table(pricesPerYearRounded)
}
const displayAveragePricePerOrder = total => {
console.log(
`%cAverage price per order: ${Math.round(
total / allOrders.length
)} ${currency}`,
'color: green; font-size: 20px'
)
}
const main = async () => {
const allOrdersFetched = await fetchAllOrders()
if (allOrdersFetched) {
console.log('All orders fetched!')
currency = allOrders[0].baseEaterOrder?.currencyCode || '€'
const mostExpensiveRestaurant = getMostExpensiveRestaurant()
const mostOrderedRestaurant = getMostOrderedRestaurant()
const totalPrice = Math.round(computeTotal(allOrders)) / 100
displayTotal(totalPrice)
displayMostOrderedRestaurant(mostOrderedRestaurant)
if (mostExpensiveRestaurant.name !== mostOrderedRestaurant.name) {
diplayMostExpensiveRestaurant(mostExpensiveRestaurant)
}
displayAveragePricePerOrder(totalPrice)
displayTotalPricesPerYear()
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment