Skip to content

Commit

Permalink
Link parameter substitutions
Browse files Browse the repository at this point in the history
- Link to documentation
- Add substitutions for flight, country, and continent
  • Loading branch information
davidfischer committed Nov 27, 2024
1 parent 9a6449f commit 4fde143
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions adserver/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ def finalize_response(self, request, response, *args, **kwargs):
response["X-Adserver-Country"] = str(request.geo.country)
response["X-Adserver-Region"] = str(request.geo.region)
response["X-Adserver-Metro"] = str(request.geo.metro)
response["X-Adserver-Continent"] = str(request.geo.continent)
response["X-Adserver-Latitude"] = str(request.geo.lat)
response["X-Adserver-Longitude"] = str(request.geo.lng)

return response
10 changes: 9 additions & 1 deletion adserver/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ class CloudflareGeoIpMiddleware(GeoIpMiddleware):
COUNTRY_HEADER = "CF-IPCountry"

# These fields will require a custom transform rule
# https://developers.cloudflare.com/rules/transform/
# https://developers.cloudflare.com/rules/transform/managed-transforms/reference/#add-visitor-location-headers
REGION_HEADER = "X-Cloudflare-Geo-Region" # ip.src.region_code
METRO_HEADER = "X-Cloudflare-Geo-Metro" # ip.src.metro_code
CONTINENT_HEADER = "X-Cloudflare-Geo-Continent" # ip.src.continent
LATITUDE_HEADER = "X-Cloudflare-Geo-Lat" # ip.src.lat
LONGITUDE_HEADER = "X-Cloudflare-Geo-Lon" # ip.src.lon

def get_geoip(self, request):
geo = super().get_geoip(request)
Expand All @@ -194,8 +197,13 @@ def get_geoip(self, request):
country_code = None

geo.country = country_code
# Region is the state/province within a country (not wider region like EU)
# See "continent"
geo.region = request.headers.get(self.REGION_HEADER, None)
geo.metro = request.headers.get(self.METRO_HEADER, None)
geo.continent = request.headers.get(self.CONTINENT_HEADER, None)
geo.lat = request.headers.get(self.LATITUDE_HEADER, None)
geo.lng = request.headers.get(self.LONGITUDE_HEADER, None)

return geo

Expand Down
23 changes: 23 additions & 0 deletions adserver/migrations/0099_link_advertiser_guide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.9 on 2024-11-27 00:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adserver', '0098_rotation_aggregation'),
]

operations = [
migrations.AlterField(
model_name='advertisement',
name='link',
field=models.URLField(help_text="URL of your landing page. This may contain UTM parameters so you know the traffic came from us. The publisher will be added in the 'ea-publisher' query parameter. Additional variable substitutions are available. See the <a href='https://www.ethicalads.io/advertiser-guide/#measuring-conversions'>advertiser guide</a>. ", max_length=1024, verbose_name='Link URL'),
),
migrations.AlterField(
model_name='historicaladvertisement',
name='link',
field=models.URLField(help_text="URL of your landing page. This may contain UTM parameters so you know the traffic came from us. The publisher will be added in the 'ea-publisher' query parameter. Additional variable substitutions are available. See the <a href='https://www.ethicalads.io/advertiser-guide/#measuring-conversions'>advertiser guide</a>. ", max_length=1024, verbose_name='Link URL'),
),
]
4 changes: 3 additions & 1 deletion adserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,9 @@ class Advertisement(TimeStampedModel, IndestructibleModel):
help_text=_(
"URL of your landing page. "
"This may contain UTM parameters so you know the traffic came from us. "
"The publisher will be added in the 'ea-publisher' query parameter."
"The publisher will be added in the 'ea-publisher' query parameter. "
"Additional variable substitutions are available. "
"See the <a href='https://www.ethicalads.io/advertiser-guide/#measuring-conversions'>advertiser guide</a>. "
),
)
image = models.ImageField(
Expand Down
1 change: 1 addition & 0 deletions adserver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class GeolocationData:
metro: int = None
lat: float = None
lng: float = None
continent: str = None


def get_ad_day():
Expand Down
5 changes: 5 additions & 0 deletions adserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,11 @@ def get_response(self, request, advertisement, publisher):
advertisement=advertisement.slug,
advertisement_slug=advertisement.slug,
advertisement_name=advertisement.name,
flight=advertisement.flight.slug,
# For privacy, don't reveal more than country/continent to advertisers
country=str(request.geo.country) if request.geo else "None",
# request.geo.region is a state/province/region inside a country
continent=str(request.geo.continent) if request.geo else "None",
)

# Append a query string param ?ea-publisher=${publisher}
Expand Down

0 comments on commit 4fde143

Please sign in to comment.