Reputation: 310
Hi all I tried to track the opened mails with PHP but google's new image proxy thing now not allowing to execute php scripts via mail(May be my imagination if someone had a working script pls point me there I tried this one https://github.com/brampauwelyn/php-email-tracker). So I'm trying that in Django with this post
https://www.pythoncircle.com/post/626/how-to-track-email-opens-sent-from-django-app/
But it seems like it's working but I can't figure out how to implement it. He skipped some of the part it. It is so confusing. Right now I have
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from mailer import views
urlpatterns = [
url(r'^image_load/$', views.image_load, name='image_load'),
path('admin/', admin.site.urls),
]
views.py
def image_load(request):
print("\nImage Loaded\n")
red = Image.new('RGB', (1, 1))
response = HttpResponse(content_type="image/png")
red.save(response, "PNG")
return response
I'm getting error at when I do this
text_content = '<h1>This is an image message.</h1>'
tracker = '<img src="{{image_url}}" alt="" width="1" height="1" border="0">'
text_content += tracker
text_content["image_url"] = HttpRequest.build_absolute_uri(reverse("image_load"))
print(context_data)
On the 4th line says
tracker['image_url'] = request.path('image_load') TypeError: 'str' object is not callable
Please help get through this.
Upvotes: 2
Views: 3296
Reputation: 11
This is pretty simple and usefull way to do that with the pytracking
package:
from pytracking import Configuration
from pytracking.django import OpenTrackingView, ClickTrackingView
class MyOpenTrackingView(OpenTrackingView):
def notify_tracking_event(self, tracking_result):
# Override this method to do something with the tracking result.
# tracking_result.request_data["user_agent"] and
# tracking_result.request_data["user_ip"] contains the user agent
# and ip of the client.
send_tracking_result_to_queue(tracking_result)
def notify_decoding_error(self, exception, request):
# Called when the tracking link cannot be decoded
# Override this to, for example, log the exception
logger.log(exception)
def get_configuration(self):
# By defaut, fetchs the configuration parameters from the Django
# settings. You can return your own Configuration object here if
# you do not want to use Django settings.
return Configuration()
class MyClickTrackingView(ClickTrackingView):
def notify_tracking_event(self, tracking_result):
# Override this method to do something with the tracking result.
# tracking_result.request_data["user_agent"] and
# tracking_result.request_data["user_ip"] contains the user agent
# and ip of the client.
send_tracking_result_to_queue(tracking_result)
def notify_decoding_error(self, exception, request):
# Called when the tracking link cannot be decoded
# Override this to, for example, log the exception
logger.log(exception)
def get_configuration(self):
# By defaut, fetchs the configuration parameters from the Django
# settings. You can return your own Configuration object here if
# you do not want to use Django settings.
return Configuration()
And then add this to urls.py:
urlpatterns = [
url(
"^open/(?P<path>[\w=-]+)/$", MyOpenTrackingView.as_view(),
name="open_tracking"),
url(
"^click/(?P<path>[\w=-]+)/$", MyClickTrackingView.as_view(),
name="click_tracking"),
]
Upvotes: 1
Reputation: 385
I did this using a slightly different method which works. In this example I track which users have opened the email but you can use another approach. In urls:
url(r"^open-tracking/(?P<user>[0-9]+)/$", PixelView.as_view(), name="pixel_view")
In the view:
import os.path
class PixelView(View):
def get(self, request, *args, **kwargs):
script_dir = os.path.dirname(os.path.abspath(__file__))
image_data = open(os.path.join(script_dir, 'static/img/open-tracking/pixel.png'), 'rb').read()
user_id = kwargs.get('user')
###Record somewhere that user_id has viewed the email
return HttpResponse(image_data, content_type="image/png")
In the email:
<img src="{{ settings.PROJECT_DOMAIN }}/open-tracking/{{ user.id }}/">
When the email loads it calls the PixelView view with the users ID. PixelView gets the users ID (which you can then use to log somewhere that the user has read the email) and then returns the pixel image to display on the email.
Few things to note, 1) the pixel/image should be a 1x1 transparent image. 2) This does not work if the email client has image loading off. 3) The email client sometimes loads the image content prior to the email actually being opened
Upvotes: 4