From d5b6dec6567ca55b9ef1b181f2f31d346f099519 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 3 Mar 2021 13:18:06 +0000 Subject: [PATCH] fixes to track_user, and StatsLog model --- stats/models.py | 1 + stats/views.py | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/stats/models.py b/stats/models.py index 0b86ec8..53441ed 100644 --- a/stats/models.py +++ b/stats/models.py @@ -22,6 +22,7 @@ class StatsLog(models.Model): geo = models.PointField('Ubicación aproximada', null=True, blank=True ) contact = models.BooleanField('Empresa contactada', null=True, blank=True) shop = models.BooleanField('Redirigido por botón "Comprar"', null=True, blank=True) + view = models.BooleanField('Redirigido por botón "Detalles"', null=True, blank=True) # internal created = models.DateTimeField('date of creation', auto_now_add=True) diff --git a/stats/views.py b/stats/views.py index 1e92f30..d5b5e45 100644 --- a/stats/views.py +++ b/stats/views.py @@ -31,40 +31,43 @@ class StatsLogViewSet(viewsets.ModelViewSet): def track_user(request): """Track user actions on the site + Three types of actions to track: + - Load product details: view=True + - Load shop URL: shop=True + - Load company profile: view=True, action_object['model'] = 'company' + Params: { - action: 'VIEW_NAME', - action_object: { - model: 'product', + 'action': 'view' | 'shop', + 'action_object': { + 'model': 'product' | 'company', id: 1, }, + 'ip': '2134.234.234.2134', + 'geo': (latitude: 324.32., longitud: 32423.23) } """ try: data = json.loads(request.body) - # geoip stuff - client_ip, is_routable = get_client_ip(request) - g = GeoIP2() - geo = None - if client_ip != '127.0.0.1': - geo = g.geos(client_ip) - # gather instance data instance_data = { 'action_object': data.get('action_object'), 'user': None if request.user.is_anonymous else request.user, 'anonymous': request.user.is_anonymous, - 'ip_address': client_ip, - 'geo': geo, + 'ip_address': data.get('ip'), + 'geo': data.get('geo'), } if data['action_object'].get('model') == 'product': instance_data['action_object'] = Product.objects.get(id=data['action_object'].get('id')) elif data['action_object'].get('model') == 'company': instance_data['action_object'] = Company.objects.get(id=data['action_object'].get('id')) - if instance_data['action_object'].shop is True: - instance_data['shop'] = True + + if data.get('action') == 'view': + instance_data['view'] = True + elif data.get('action') == 'shop': + instance_data['shop'] = True # crate new instance new_stat = StatsLog.objects.create(**instance_data)