Blog

Probabilistic revenue sharing

May 26, 2020

On each page, only one monetization tag is allowed. To share revenue among multiple people, you can randomly choose a pointer based on weights and set the tag dynamically. The visitor pays to the chosen pointer until the page is reloaded or closed.

import React, { useEffect, useState } from "react"
import { Helmet } from "react-helmet/es/Helmet"

// Define the payment pointers and assign each one a weight.
// If the weights total 100, then they represent the percentage each pointer gets.
const pointers = {
  "$alice.example": 50,
  "$bob.example": 40,
  "$connie.example": 9.5,
  "$dave.example": 0.5,
}

// Choose pointer randomly based on the values above
function pickPointer() {
  const sum = Object.values(pointers).reduce((sum, weight) => sum + weight, 0)
  let choice = Math.random() * sum

  for (const pointer in pointers) {
    const weight = pointers[pointer]
    if ((choice -= weight) <= 0) {
      return pointer
    }
  }
}

export const RevenueSharing = () => {
  const [wallet, setWallet] = useState()

  useEffect(() => {
    setWallet(pickPointer())
  }, [])

  return (
    <div>
      {/*Insert the chosen pointer to <head>*/}
      <Helmet>
        <meta name="monetization" content={wallet}/>
      </Helmet>
      <p>Current wallet being used: {wallet}</p>
    </div>
  )
}

Demo

Current wallet being used:


Created by Petr Janík, a student of informatics in Brno, Czech Republic and a huge fan of web development. Follow me Twitter