Skip to content

Commit

Permalink
location test
Browse files Browse the repository at this point in the history
  • Loading branch information
patra0o committed Apr 20, 2024
1 parent 8f3af14 commit 77aaca5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/index.js
index.js
/node_modules
/package-lock.json
/package.json
package.json
29 changes: 24 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ <h1 class="mb-3">Advertisement Tracker</h1>
document.addEventListener('DOMContentLoaded', function () {
const adBanner = document.getElementById('ad-banner');
const adId = 150; // Retrieve the adId
const site = 'https://8b7aa341-4416-4671-b9bc-03c0e393c6c8-00-1r6zz1ux729b8.janeway.replit.dev';
//const site = 'https://8b7aa341-4416-4671-b9bc-03c0e393c6c8-00-1r6zz1ux729b8.janeway.replit.dev';
// or set to localhost
//const site = 'http://localhost';
const site = 'http://localhost';
// Function to send data to the API
function sendDataToAPI(eventType, additionalData) {
fetch(`${site}:3000/ad-event`, {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
eventType,
adId, // Include the adId in the data sent
adId, // Include the adId in the data sent to be made dynamic
...additionalData,
timestamp: new Date().toISOString(),
}),
Expand All @@ -66,9 +65,29 @@ <h1 class="mb-3">Advertisement Tracker</h1>
};
}

function getLocationAndSend(eventType) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
const locationData = {
lat: position.coords.latitude,
lon: position.coords.longitude
};
sendDataToAPI(eventType, locationData);
}, error => {
console.error('Error obtaining location:', error);
sendDataToAPI(eventType, { error: 'Location permission denied or unavailable' });
});
} else {
console.log('Geolocation is not supported by this browser.');
sendDataToAPI(eventType, { error: 'Geolocation not supported' });
}
}

// Event handlers
adBanner.addEventListener('mouseover', () => sendDataToAPI('hover', collectClientData()));
adBanner.addEventListener('click', () => sendDataToAPI('click', collectClientData()));
adBanner.addEventListener('mouseover', () => getLocationAndSend('hover'));
adBanner.addEventListener('click', () => getLocationAndSend('click'));

// Visibility tracking for 'view' events
const observer = new IntersectionObserver((entries) => {
Expand All @@ -86,4 +105,4 @@ <h1 class="mb-3">Advertisement Tracker</h1>
crossorigin="anonymous"></script>
</body>

</html>
</html>

0 comments on commit 77aaca5

Please sign in to comment.