change URL based on city/region
cheaunknown
javascript
2 years ago
1.6 kB
5
Indexable
<script>
// Easily modifiable variables
const apiKey = '01e4ac7e3608403cbb51ec0ebbf0f6fd'; // Replace with your API key
const originalUrl = 'https://track.americanlifenow.com/click';
const newUrl = 'https://www.google.com';
const targetCities = [
"Atlanta, Georgia",
"Boston, Massachusetts",
"Chicago, Illinois",
"Dallas, Texas",
"Denver, Colorado",
"Kansas City, Missouri",
"New York, New York",
"Philadelphia, Pennsylvania",
"San Francisco, California",
"Seattle, Washington"
];
const targetState = "Florida"; // Targeting the entire state
async function getUserLocation() {
try {
const response = await fetch(`https://ipgeolocation.abstractapi.com/v1/?api_key=${apiKey}`);
const data = await response.json();
return { city: data.city, state: data.region }; // Return both city and state
} catch (error) {
console.error('Error fetching user location:', error);
return null;
}
}
async function updateLinksBasedOnLocation() {
const userLocation = await getUserLocation();
if (userLocation) {
const cityStateCombination = `${userLocation.city}, ${userLocation.state}`;
if (targetCities.includes(cityStateCombination) || userLocation.state === targetState) {
document.querySelectorAll(`a[href="${originalUrl}"]`).forEach(link => {
link.href = newUrl;
});
}
}
}
document.addEventListener('DOMContentLoaded', updateLinksBasedOnLocation);
</script>
Editor is loading...
Leave a Comment