Skip to content

Commit 227b706

Browse files
committed
strapi integration
1 parent 41d5b06 commit 227b706

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed

Diff for: ad-server.js

+56-51
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,77 @@
11
const cors = require('cors');
22
const express = require('express');
3-
const bodyParser = require('body-parser'); // parses the https body
4-
const axios = require('axios'); // does a fetch request to geolocation api
5-
3+
const bodyParser = require('body-parser');
4+
const axios = require('axios');
65
const app = express();
76

87
app.use(bodyParser.json());
98
app.use(cors());
109
app.options("*", cors());
1110

12-
// Initialize the eventCounter object to store the counts
11+
const STRAPI_BASE_URL = 'http://localhost:1337/api/ad-events'; // Strapi endpoint
12+
const STRAPI_TOKEN = 'dae13e6e613a06ea51e569752f6c7bb35e011d783b7f19ed30e1036d13af10dca0794d9d766e804bad7b58959d6ef84d29227677e1f183b2427ced66d810828902477103e2cb41bcab528ea38b817c7ef07575a25e05719e355da0c444f7309e2bce1ac478b43ada51834796279f6ae730dce9fb62207a1f1c020fc152e81932'; // Strapi API token
13+
1314
const eventCounter = {};
1415

1516
app.post('/ad-event', async (req, res) => {
16-
// we collect eventTypes of click, view and hover
17-
// we also collect device size, time
18-
// adId needs to be made dynamic
19-
const { eventType, location, device, screenSize, timestamp, adId } = req.body;
20-
21-
// Geolocation API endpoint configuration
22-
const url = `http://ip-api.com/json/?fields=status,message,country,regionName,city`;
23-
24-
17+
const { eventType, browser, screenSize, timestamp, adId, device } = req.body;
18+
const geoURL = `http://ip-api.com/json/?fields=status,message,country,regionName,city`;
19+
const stringAdID = adId.toString();
2520
try {
26-
const response = await axios.get(url);
27-
const geoData = response.data;
21+
const geoResponse = await axios.get(geoURL);
22+
const geoData = geoResponse.data;
23+
24+
if (geoData.status !== 'success') {
25+
return res.status(400).json({ message: 'Geolocation data fetch failed', details: geoData.message });
26+
}
2827

28+
// Initialize event count if not existing
29+
if (!eventCounter[adId]) {
30+
eventCounter[adId] = {};
31+
}
32+
if (!eventCounter[adId][eventType]) {
33+
eventCounter[adId][eventType] = 0;
34+
}
35+
eventCounter[adId][eventType] += 1;
2936

30-
if (geoData.status === 'success') {
31-
// Ensure there is an object for the adId
32-
if (!eventCounter[adId]) {
33-
eventCounter[adId] = {};
34-
}
35-
// Ensure there is a counter for the specific eventType
36-
if (!eventCounter[adId][eventType]) {
37-
eventCounter[adId][eventType] = 0;
38-
}
39-
eventCounter[adId][eventType] += 1;
37+
const eventData = {
38+
eventType,
39+
browser,
40+
screenSize,
41+
timestamp,
42+
stringAdID,
43+
country: geoData.country,
44+
region: geoData.regionName,
45+
city: geoData.city,
46+
eventCounts: eventCounter[adId][eventType].toString(),
47+
device, // Convert count to string if necessary
48+
};
4049

41-
// Enhanced log with geolocation data
42-
console.log(JSON.stringify({
43-
message: "Event Received and Processed",
44-
eventType: eventType,
45-
location: location,
46-
device: device,
47-
screenSize: screenSize,
48-
timestamp: timestamp,
49-
country: geoData.country,
50-
region: geoData.regionName,
51-
city: geoData.city,
52-
eventCounts: eventCounter[adId][eventType],
53-
adId: adId
54-
}));
50+
// Post eventData to Strapi
51+
const url = STRAPI_BASE_URL
52+
const data = eventData;
53+
const customHeaders = {
54+
"Content-Type": "application/json",
55+
}
5556

56-
res.json({
57-
message: `Event logged for ${adId} with type ${eventType}`,
58-
location: {
59-
country: geoData.country,
60-
region: geoData.regionName,
61-
city: geoData.city
62-
}
57+
fetch(url, {
58+
method: "POST",
59+
headers: customHeaders,
60+
body: JSON.stringify({ data: data }),
61+
})
62+
.then((response) => response.json())
63+
.then((data) => {
64+
console.log(data);
6365
});
64-
} else {
65-
throw new Error(geoData.message);
66-
}
66+
67+
// Respond with success and the data returned from Strapi
68+
res.json({
69+
message: `Event logged successfully ${eventData}`,
70+
});
71+
console.log(`Event logged successfully ${eventData}`);
6772
} catch (error) {
68-
console.error('Failed to get or process geolocation data:', error);
69-
res.status(500).send('Geolocation api call timeout');
73+
console.error('Failed to process request:', error);
74+
res.status(500).json({ message: 'Internal Server Error', error: error.message });
7075
}
7176
});
7277

Diff for: index.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ <h1 class="mb-3">Advertisement Tracker</h1>
6060
function collectClientData() {
6161
return {
6262
location: window.location.href,
63-
device: navigator.userAgent,
63+
device: window.navigator.platform,
64+
browser: navigator.userAgent,
6465
screenSize: `${window.innerWidth} x ${window.innerHeight}`,
6566
};
6667
}
6768

6869

6970
// Event handlers
70-
adBanner.addEventListener('mouseover', () => sendDataToAPI('hover', collectClientData()));
71+
// adBanner.addEventListener('mouseover', () => sendDataToAPI('hover', collectClientData()));
7172
adBanner.addEventListener('click', () => sendDataToAPI('click', collectClientData()));
7273

7374
// Visibility tracking for 'view' events

0 commit comments

Comments
 (0)