Minimum example of dynamically enabling web monetisation based off of GeoIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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