|
1 | 1 | const cors = require('cors');
|
2 | 2 | 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'); |
6 | 5 | const app = express();
|
7 | 6 |
|
8 | 7 | app.use(bodyParser.json());
|
9 | 8 | app.use(cors());
|
10 | 9 | app.options("*", cors());
|
11 | 10 |
|
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 | + |
13 | 14 | const eventCounter = {};
|
14 | 15 |
|
15 | 16 | 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(); |
25 | 20 | 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 | + } |
28 | 27 |
|
| 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; |
29 | 36 |
|
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 | + }; |
40 | 49 |
|
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 | + } |
55 | 56 |
|
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); |
63 | 65 | });
|
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}`); |
67 | 72 | } 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 }); |
70 | 75 | }
|
71 | 76 | });
|
72 | 77 |
|
|
0 commit comments