Skip to content

Commit 17eb961

Browse files
committed
feat: Inject geo information into Prebid configuration and refactor geo header setting into the struct
1 parent 0255543 commit 17eb961

File tree

3 files changed

+38
-71
lines changed

3 files changed

+38
-71
lines changed

crates/common/src/geo.rs

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use fastly::Request;
88

99
use crate::constants::{
1010
HEADER_X_GEO_CITY, HEADER_X_GEO_CONTINENT, HEADER_X_GEO_COORDINATES, HEADER_X_GEO_COUNTRY,
11-
HEADER_X_GEO_INFO_AVAILABLE, HEADER_X_GEO_METRO_CODE,
11+
HEADER_X_GEO_METRO_CODE,
1212
};
1313

1414
/// Geographic information extracted from a request.
@@ -81,74 +81,26 @@ impl GeoInfo {
8181
pub fn has_metro_code(&self) -> bool {
8282
self.metro_code > 0
8383
}
84-
}
85-
86-
/// Extracts the DMA (Designated Market Area) code from the request's geolocation data.
87-
///
88-
/// This function:
89-
/// 1. Checks if running in Fastly environment
90-
/// 2. Performs geo lookup based on client IP
91-
/// 3. Sets various geo headers on the request
92-
/// 4. Returns the metro code (DMA) if available
93-
///
94-
/// # Arguments
95-
///
96-
/// * `req` - The request to extract DMA code from
97-
///
98-
/// # Returns
99-
///
100-
/// The DMA/metro code as a string if available, None otherwise
101-
pub fn get_dma_code(req: &mut Request) -> Option<String> {
102-
// Debug: Check if we're running in Fastly environment
103-
log::info!("Fastly Environment Check:");
104-
log::info!(
105-
" FASTLY_POP: {}",
106-
std::env::var("FASTLY_POP").unwrap_or_else(|_| "not in Fastly".to_string())
107-
);
108-
log::info!(
109-
" FASTLY_REGION: {}",
110-
std::env::var("FASTLY_REGION").unwrap_or_else(|_| "not in Fastly".to_string())
111-
);
112-
113-
// Get detailed geo information using geo_lookup
114-
if let Some(geo) = req.get_client_ip_addr().and_then(geo_lookup) {
115-
log::info!("Geo Information Found:");
11684

117-
// Set all available geo information in headers
118-
let city = geo.city();
119-
req.set_header(HEADER_X_GEO_CITY, city);
120-
log::info!(" City: {}", city);
121-
122-
let country = geo.country_code();
123-
req.set_header(HEADER_X_GEO_COUNTRY, country);
124-
log::info!(" Country: {}", country);
125-
126-
req.set_header(HEADER_X_GEO_CONTINENT, format!("{:?}", geo.continent()));
127-
log::info!(" Continent: {:?}", geo.continent());
128-
129-
req.set_header(
130-
HEADER_X_GEO_COORDINATES,
131-
format!("{},{}", geo.latitude(), geo.longitude()),
132-
);
133-
log::info!(" Location: ({}, {})", geo.latitude(), geo.longitude());
134-
135-
// Get and set the metro code (DMA)
136-
let metro_code = geo.metro_code();
137-
req.set_header(HEADER_X_GEO_METRO_CODE, metro_code.to_string());
138-
log::info!("Found DMA/Metro code: {}", metro_code);
139-
return Some(metro_code.to_string());
140-
} else {
141-
log::info!("No geo information available for the request");
142-
req.set_header(HEADER_X_GEO_INFO_AVAILABLE, "false");
143-
}
144-
145-
// If no metro code is found, log all request headers for debugging
146-
log::info!("No DMA/Metro code found. All request headers:");
147-
for (name, value) in req.get_headers() {
148-
log::info!(" {}: {:?}", name, value);
85+
/// Sets the geographic information headers on the given request.
86+
///
87+
/// This sets the following headers:
88+
/// - `x-geo-city`
89+
/// - `x-geo-country`
90+
/// - `x-geo-continent`
91+
/// - `x-geo-coordinates`
92+
/// - `x-geo-metro-code`
93+
/// - `x-geo-region` (if available)
94+
pub fn set_headers(&self, req: &mut Request) {
95+
req.set_header(HEADER_X_GEO_CITY, &self.city);
96+
req.set_header(HEADER_X_GEO_COUNTRY, &self.country);
97+
req.set_header(HEADER_X_GEO_CONTINENT, &self.continent);
98+
req.set_header(HEADER_X_GEO_COORDINATES, self.coordinates_string());
99+
req.set_header(HEADER_X_GEO_METRO_CODE, self.metro_code.to_string());
100+
if let Some(region) = &self.region {
101+
req.set_header("x-geo-region", region);
102+
}
149103
}
150-
151-
None
152104
}
153105

154106
/// Returns the geographic information for the request as a JSON response.

crates/common/src/integrations/prebid.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl crate::integrations::IntegrationHtmlPostProcessor for PrebidHtmlInjector {
228228
ctx: &crate::integrations::IntegrationHtmlContext<'_>,
229229
) -> bool {
230230
// Construct the Prebid configuration object
231-
let config = json!({
231+
let mut config = json!({
232232
"accountId": "trusted-server",
233233
"enabled": true,
234234
"bidders": self.config.bidders,
@@ -242,6 +242,19 @@ impl crate::integrations::IntegrationHtmlPostProcessor for PrebidHtmlInjector {
242242
"debug": self.config.debug,
243243
});
244244

245+
// Inject Geo information if available
246+
if let Some(geo) = ctx.geo {
247+
config["geo"] = json!({
248+
"city": geo.city,
249+
"country": geo.country,
250+
"continent": geo.continent,
251+
"lat": geo.latitude,
252+
"lon": geo.longitude,
253+
"metroCode": geo.metro_code,
254+
"region": geo.region,
255+
});
256+
}
257+
245258
// Script to inject configuration and initialize Prebid via tsjs
246259
let script = format!(
247260
r#"<script>

crates/common/src/publisher.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,15 @@ pub fn handle_publisher_request(
226226
)?;
227227
let origin_host = settings.publisher.origin_host();
228228

229-
// Inject Geo headers for the backend request
230-
crate::geo::get_dma_code(&mut req);
231-
232229
// Extract GeoInfo from request before it is consumed
233230
use crate::geo::GeoInfo;
234231
let geo_info = GeoInfo::from_request(&req);
235232

233+
// Inject Geo headers for the backend request if available
234+
if let Some(geo) = &geo_info {
235+
geo.set_headers(&mut req);
236+
}
237+
236238
// Capture Geo headers to copy to response
237239
let geo_headers: Vec<(String, String)> = [
238240
"x-geo-city",

0 commit comments

Comments
 (0)