Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.3 KB
Raw Blame
import { PluginEvent } from '~/plugin-scaffold'

import { LegacyTransformationPluginMeta } from '../../types'
import DATA from './rates.json'

const RATES = DATA.rates as Record<string, number>

export function processEvent(event: PluginEvent, meta: LegacyTransformationPluginMeta) {
    const {
        openExchangeRatesApiKey,
        normalizedCurrency,
        amountProperty,
        currencyProperty,
        normalizedAmountProperty,
        normalizedCurrencyProperty,
    } = meta.config

    if (
        openExchangeRatesApiKey &&
        normalizedCurrency &&
        event?.properties &&
        typeof event.properties[amountProperty] !== 'undefined' &&
        typeof event.properties[currencyProperty] !== 'undefined'
    ) {
        const amount = event.properties[amountProperty]
        const currency = event.properties[currencyProperty]

        if (RATES[currency] && RATES[normalizedCurrency]) {
            const normalizedAmount = roundToDigits((amount * RATES[normalizedCurrency]) / RATES[currency], 4)
            event.properties[normalizedAmountProperty] = normalizedAmount
            event.properties[normalizedCurrencyProperty] = normalizedCurrency
        }
    }

    return event
}

function roundToDigits(number: number, digits: number) {
    return Math.round(number * Math.pow(10, digits)) / Math.pow(10, digits)
}