Skip to content

Commit

Permalink
Merge pull request #1 from 3k3n3/fix/impressions
Browse files Browse the repository at this point in the history
Fixed impressions and clicks not showing for anonymous users
  • Loading branch information
3k3n3 authored Sep 6, 2023
2 parents 3d981d9 + 335b042 commit 6f50029
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ads/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
default_app_config = 'ads.apps.AdsConfig'

__version__ = '1.2.0'
__version__ = '1.2.1'
34 changes: 19 additions & 15 deletions ads/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

def get_zones_choices():
for key in sorted(settings.ADS_ZONES):
yield (key, _(settings.ADS_ZONES[key].get('name', 'Undefined')))
yield (key, _(settings.ADS_ZONES[key].get("name", "Undefined")))


def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', None)
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR", None)
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
ip = x_forwarded_for.split(",")[0]
else:
ip = request.META.get('REMOTE_ADDR', '')
ip = request.META.get("REMOTE_ADDR", "")
return ip


Expand All @@ -26,18 +26,22 @@ def update_clicks(ad, request):
ad=ad,
session_id=request.session.session_key,
defaults={
'click_date': timezone.now(),
'source_ip': get_client_ip(request),
})
"click_date": timezone.now(),
"source_ip": get_client_ip(request),
},
)


def update_impressions(ad, request):
if ad is not None:
if request.session.session_key:
impression, created = Impression.objects.get_or_create(
ad=ad,
session_id=request.session.session_key,
defaults={
'impression_date': timezone.now(),
'source_ip': get_client_ip(request),
})
# if request.session.session_key:
request.session["django-ads"] = True
request.session.save()
impression, created = Impression.objects.get_or_create(
ad=ad,
session_id=request.session.session_key,
defaults={
"impression_date": timezone.now(),
"source_ip": get_client_ip(request),
},
)
45 changes: 20 additions & 25 deletions ads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,44 @@


class AdImpressionView(JSONResponseMixin, View):
json_dumps_kwargs = {u"indent": 2}
json_dumps_kwargs = {"indent": 2}

def get_object(self):
zone = self.kwargs.get('zone', None)
zone = self.kwargs.get("zone", None)
return Ad.objects.random_ad(zone)

def get_ad_context_dict(self, zone):
ad = Ad.objects.random_ad(zone)
if ad:
context_dict = {
'url': ad.get_absolute_url(),
'images': {}
}
context_dict = {"url": ad.get_absolute_url(), "images": {}}
for image in ad.images.all():
context_dict['images'].update({
image.device: {
'url': image.image.url,
'size': image.size
}
})
context_dict["images"].update(
{image.device: {"url": image.image.url, "size": image.size}}
)
return context_dict
return None

def get(self, request, *args, **kwargs):
data = {}
zones = request.GET.getlist('zones[]', []);
zones = request.GET.getlist("zones[]", [])
for zone in zones:
zone_conf = settings.ADS_ZONES.get(zone, {})
ad = self.get_ad_context_dict(zone)

if ad:
# Extract ad ID and update impressions each time ad is displayed
ads = Ad.objects.get(id=ad["url"][5:-1])
update_impressions(ads, request)

if zone_conf:
data.update({
zone: {
'ad': ad,
'conf': zone_conf
}
})
data.update({zone: {"ad": ad, "conf": zone_conf}})
context_dict = {
'google_adsense_client': settings.ADS_GOOGLE_ADSENSE_CLIENT,
'viewports': settings.ADS_VIEWPORTS,
'zones': data
"google_adsense_client": settings.ADS_GOOGLE_ADSENSE_CLIENT,
"viewports": settings.ADS_VIEWPORTS,
"zones": data,
}
return self.render_json_response(context_dict)

"""
context_dict = {
'zone': self.kwargs.get('zone', None)
Expand All @@ -74,8 +69,8 @@ def get(self, request, *args, **kwargs):
return self.render_json_response(context_dict)
"""

class AdClickView(SingleObjectMixin, View):

class AdClickView(SingleObjectMixin, View):
def get_queryset(self):
return Ad.objects.all()

Expand Down

0 comments on commit 6f50029

Please sign in to comment.