Skip to content

Instantly share code, notes, and snippets.

@i-a-i-n
Created August 5, 2022 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-a-i-n/b012c99cef7298c00f8cc03b0d98e710 to your computer and use it in GitHub Desktop.
Save i-a-i-n/b012c99cef7298c00f8cc03b0d98e710 to your computer and use it in GitHub Desktop.
Minimum example of dynamically enabling web monetisation based off of GeoIP
const key = ""
const ip = ""
const payment_pointer = ""
const monetised_county_codes = [""]
const getLocation = (ip, key) => {
const URL = `http://ipinfo.io/${ip}?token=${key}`;
return fetch(URL).then(
(response) => {return response.json()}
).then(
(jsonResponse) => {return jsonResponse.country}
)
}
const injectMonetisation = (payment_pointer) => {
const meta = document.createElement('meta')
meta.name = 'monetization'
meta.content = payment_pointer
document.getElementsByTagName('head')[0].appendChild(meta)
}
const triggerMonetisation = (location, payment_pointer) => {
if (monetised_county_codes.includes(location)) injectMonetisation(payment_pointer)
}
getLocation(ip, key).then(location => triggerMonetisation(location, payment_pointer))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment