-
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
141 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": "14.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,52 @@ | ||
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, | ||
} | ||
) | ||
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 | ||
""" | ||
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>") | ||
|
||
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") | ||
|
||
for a_tag in soup.find_all("a", href=True): | ||
original_url = a_tag["href"] | ||
|
||
tracked_url = f"/mail/track/click/{trace_id}?\ | ||
redirect_url={tools.url_quote(original_url)}" | ||
|
||
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,19 @@ | ||
from odoo import fields, models | ||
|
||
|
||
class MailingTrace(models.Model): | ||
_inherit = "mailing.trace" | ||
|
||
mail_message_id = fields.Many2one( | ||
"mail.message", | ||
string="Chatter Message", | ||
help="The chatter message related to this email trace.", | ||
) | ||
|
||
mail_message_id_int = fields.Integer( | ||
string="Message ID (tech)", | ||
help="ID of the related mail_message. This field is an integer field because " | ||
"the related mail_message can be deleted separately from its statistics. " | ||
"However the ID is needed for several action and controllers.", | ||
index=True, | ||
) |
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, | ||
) |