Skip to content

Fix view/click date to date to offer #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions adserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,14 +1713,20 @@ def generate_slug(cls, name):
def advertiser(self):
return self.flight.campaign.advertiser

def incr(self, impression_type, publisher):
def incr(self, impression_type, publisher, offer=None):
"""
Add to the number of times this action has been performed, stored in the DB.

TODO: Refactor this method, moving it off the Advertisement class since it can be called
without an advertisement when we have a Decision and no Offer.
"""
day = get_ad_day().date()
if offer:
# For views/clicks, use the date of the existing offer
day = offer.date.date()
else:
# If we don't have an offer (the offer is about to be created),
# we use the current date
day = get_ad_day().date()

if isinstance(impression_type, str):
impression_types = (impression_type,)
Expand Down Expand Up @@ -1834,7 +1840,7 @@ def track_impression(self, request, impression_type, publisher, offer):

def track_click(self, request, publisher, offer):
"""Store click data in the DB."""
self.incr(impression_type=CLICKS, publisher=publisher)
self.incr(impression_type=CLICKS, publisher=publisher, offer=offer)
return self._record_base(
request=request,
model=Click,
Expand All @@ -1856,7 +1862,7 @@ def track_view(self, request, publisher, offer):
For a large scale ad server, writing a database record per ad view
is not feasible
"""
self.incr(impression_type=VIEWS, publisher=publisher)
self.incr(impression_type=VIEWS, publisher=publisher, offer=offer)

if request.GET.get("uplift"):
# Don't overwrite Offer object here, since it might have changed prior to our writing
Expand Down Expand Up @@ -1916,7 +1922,6 @@ def offer_ad(
"""
ad_type = AdType.objects.filter(slug=ad_type_slug).first()

self.incr(impression_type=(OFFERS, DECISIONS), publisher=publisher)
offer = self._record_base(
request=request,
model=Offer,
Expand All @@ -1928,6 +1933,7 @@ def offer_ad(
paid_eligible=paid_eligible,
rotations=rotations,
)
self.incr(impression_type=(OFFERS, DECISIONS), publisher=publisher, offer=offer)

if forced and self.flight.campaign.campaign_type == PAID_CAMPAIGN:
# Ad offers forced to a specific ad or campaign should never be billed.
Expand Down Expand Up @@ -2007,8 +2013,7 @@ def record_null_offer(
Without this, when we don't offer an ad and a user doesn't have house ads on,
we don't have any way to track how many requests for an ad there have been.
"""
cls.incr(self=None, impression_type=DECISIONS, publisher=publisher)
cls._record_base(
offer = cls._record_base(
self=None,
request=request,
model=Offer,
Expand All @@ -2019,6 +2024,7 @@ def record_null_offer(
ad_type_slug=ad_type_slug,
paid_eligible=paid_eligible,
)
cls.incr(self=None, impression_type=DECISIONS, publisher=publisher, offer=offer)

def is_valid_offer(self, impression_type, offer):
"""
Expand Down