-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "Mail Chatter Statistics", | ||
"version": "16.0.1.0.0", | ||
"author": "Avanzosc", | ||
"summary": "Add email tracking functionality to Odoo chatter.", | ||
"website": "https://github.com/avanzosc/odoo-addons", | ||
"license": "LGPL-3", | ||
"depends": ["mail", "mass_mailing"], | ||
"data": [ | ||
"views/mail_mail_views.xml", | ||
"views/mailing_trace_views.xml", | ||
], | ||
"installable": True, | ||
"application": False, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import mail_track |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from odoo.http import Controller, request, route | ||
|
||
|
||
class MailTrackController(Controller): | ||
@route("/mail/track/click/<int:trace_id>", type="http", auth="public", website=True) | ||
def track_click(self, trace_id, redirect_url=None, **kwargs): | ||
""" | ||
Logs the click event and redirects the user to the original URL | ||
""" | ||
trace = request.env["mailing.trace"].sudo().browse(trace_id) | ||
if trace: | ||
trace.write({"status": "clicked"}) | ||
|
||
if redirect_url: | ||
return request.redirect(redirect_url) | ||
return request.redirect("/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import mail_mail | ||
from . import mailing_trace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from bs4 import BeautifulSoup | ||
|
||
from odoo import models, tools | ||
|
||
|
||
class MailMail(models.Model): | ||
_inherit = "mail.mail" | ||
|
||
def send(self, auto_commit=False, raise_exception=False): | ||
res = super().send(auto_commit=auto_commit, raise_exception=raise_exception) | ||
|
||
for mail in self: | ||
trace = self.env["mailing.trace"].create( | ||
{ | ||
"mail_id": mail.id, | ||
"email": mail.email_to, | ||
"status": "sent", | ||
"message_id": mail.message_id, | ||
} | ||
) | ||
# Add tracking pixel and replace links with tracked versions | ||
mail.body_html = self._add_tracking(mail.body_html, trace.id) | ||
trace.write({"status": "tracking_added"}) | ||
|
||
return res | ||
|
||
def _add_tracking(self, body_html, trace_id): | ||
""" | ||
Add tracking pixel and replace links for click tracking | ||
""" | ||
# Add tracking pixel | ||
tracking_pixel = f'<img src="/mail/track/open/{trace_id}" width="1" height="1" style="display:none"/>' | ||
body_html = body_html.replace("</body>", f"{tracking_pixel}</body>") | ||
|
||
# Replace links with tracked links | ||
body_html = self._replace_links_with_tracked(body_html, trace_id) | ||
|
||
return body_html | ||
|
||
def _replace_links_with_tracked(self, body_html, trace_id): | ||
""" | ||
Replace all the hyperlinks in the email with tracked versions | ||
""" | ||
soup = BeautifulSoup(body_html, "html.parser") | ||
|
||
# Find all anchor tags in the email body | ||
for a_tag in soup.find_all("a", href=True): | ||
original_url = a_tag["href"] | ||
|
||
# Create a tracked link | ||
tracked_url = f"/mail/track/click/{trace_id}?redirect_url={tools.url_quote(original_url)}" | ||
|
||
# Replace the original URL with the tracked one | ||
a_tag["href"] = tracked_url | ||
|
||
return str(soup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from odoo import fields, models | ||
|
||
|
||
class MailingTrace(models.Model): | ||
_name = "mailing.trace" | ||
_description = "Email Tracking for Chatter" | ||
|
||
mail_id = fields.Many2one("mail.mail", string="Mail") | ||
email = fields.Char(string="Email") | ||
message_id = fields.Char(string="Message ID") | ||
status = fields.Selection( | ||
[ | ||
("sent", "Sent"), | ||
("tracking_added", "Tracking Added"), | ||
("opened", "Opened"), | ||
("clicked", "Clicked"), | ||
("bounced", "Bounced"), | ||
], | ||
string="Status", | ||
) | ||
open_count = fields.Integer(string="Open Count", default=0) | ||
click_count = fields.Integer(string="Click Count", default=0) | ||
|
||
def track_open(self): | ||
""" | ||
Método que incrementa el contador de aperturas. | ||
""" | ||
self.open_count += 1 | ||
self.status = "opened" | ||
|
||
def track_click(self): | ||
""" | ||
Método que incrementa el contador de clics. | ||
""" | ||
self.click_count += 1 | ||
self.status = "clicked" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<odoo> <record id="view_mail_form_inherit_chatter_stats" model="ir.ui.view"> | ||
<field name="name">mail.mail.form.view.stats</field> | ||
<field name="model">mail.mail</field> | ||
<field name="inherit_id" ref="mail.view_mail_mail_form" /> | ||
<field name="arch" type="xml"> | ||
<xpath expr="//header" position="inside"> | ||
<button | ||
name="%(action_show_tracking)d" | ||
string="Show Email Tracking" | ||
type="action" | ||
class="btn-primary" | ||
/> | ||
</xpath> | ||
</field> | ||
</record> | ||
|
||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<odoo> | ||
<record id="action_show_tracking" model="ir.actions.act_window"> | ||
<field name="name">Email Tracking</field> | ||
<field name="res_model">mailing.trace</field> | ||
<field name="view_mode">tree,form</field> | ||
<field name="target">new</field> | ||
</record> | ||
</odoo> |
1 change: 1 addition & 0 deletions
1
setup/mail_chatter_statistics/odoo/addons/mail_chatter_statistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../mail_chatter_statistics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |