Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #141 and add custom event trigger binding support #193

Closed
wants to merge 8 commits into from
Closed
17 changes: 15 additions & 2 deletions analytical/templatetags/matomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
get_identity,
get_required_setting,
is_internal_ip,
build_paq_cmd,
get_event_bind_js
)

# domain name (characters separated by a dot), optional port, optional URI path, no slash
Expand Down Expand Up @@ -48,10 +50,8 @@

MatomoVar = namedtuple('MatomoVar', ('index', 'name', 'value', 'scope'))


register = Library()


@register.tag
def matomo(parser, token):
"""
Expand Down Expand Up @@ -97,6 +97,19 @@ def render(self, context):
if getattr(settings, 'MATOMO_DISABLE_COOKIES', False):
commands.append(DISABLE_COOKIES_CODE)

if getattr(settings, "MATOMO_REQUIRE_CONSENT", False):
SilverStrings024 marked this conversation as resolved.
Show resolved Hide resolved
grant_class_name = settings.GRANT_CONSENT_TAG_CLASSNAME
SilverStrings024 marked this conversation as resolved.
Show resolved Hide resolved
revoke_class_name = settings.REVOKE_CONSENT_CLASSNAME
commands.append(build_paq_cmd('requireConsent'))
commands.append(get_event_bind_js(
class_name=grant_class_name,
matomo_event="rememberConsentGiven",
))
commands.append(get_event_bind_js(
class_name=revoke_class_name,
matomo_event="forgetConsentGiven",
))

userid = get_identity(context, 'matomo')
if userid is not None:
variables_code = chain(variables_code, (
Expand Down
53 changes: 53 additions & 0 deletions analytical/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,56 @@ class AnalyticalException(Exception):
be silenced in templates.
"""
silent_variable_failure = True

def build_paq_cmd(cmd, args=[]):
"""
:Args:
- cmd -> The command to be pushed to paq (i.e enableHeartbeatTimer or contentInteraction)
- args -> Arguments to be added to the paq command. This is mainly
used when building commands to be used on manual event trigger.

:Returns:
A complete '_paq.push([])' command in string form
"""
paq = "_paq.push(['%s', " % (cmd)
if len(args) > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(args) > 0:
if args:

... is pythonic.

for arg_idx in range(len(args)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this pythonic, e.g.

Suggested change
for arg_idx in range(len(args)):
for item in args:

if arg_idx == len(args)-1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is – and the else black below – is not an elegant solution. Why don't you simply always add the ", " and replace it by "]);" after the end of the loop? e.g.

   paq = paq[:-3)] + "]);"
   return paq

paq += "'%s'])" % (args[arg_idx])
SilverStrings024 marked this conversation as resolved.
Show resolved Hide resolved
else:
paq += "'%s', " % (args[arg_idx])
else:
paq += "])"
return paq

def get_event_bind_js(
class_name, matomo_event,
matomo_args=[], js_event="onclick",
):
Comment on lines +223 to +226
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_event_bind_js(
class_name, matomo_event,
matomo_args=[], js_event="onclick",
):
def get_event_bind_js(
class_name, matomo_event, matomo_args=[], js_event="onclick"
):

"""
Build a javascript command to bind an onClick event to some
element whose handler pushes something to _paq
:Args:
- class_name: Value of the 'class' attribute of the tag
the event is to be bound to.
- matomo_event: The matomo event to be pushed to _paq
such as enableHeartbeatTimer or contentInteraction
- matomo_args: The arguments to be passed with the matomo event
meaning
:Return:
A string of javascript that loops the elements found by
document.getElementByClassName and binds the motomo event
to each element that was found
"""
script = f"""
var elems = document.getElementByClassName('%s');
for (var i=0; i++; i < elems.length){{
elems[i].addEventListener('%s',
function(){{
%s;
}}
);
}}
""" % (class_name, js_event, build_paq_cmd(matomo_event, matomo_args))
return script