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

Track ebook clicks from menu #1753

Merged
merged 8 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<label for="table-of-contents-switcher-{{switcher_name}}" class="visually-hidden">
{{ self.table_of_contents_switcher() }}
</label>
<select id="table-of-contents-switcher-{{switcher_name}}">
<select id="table-of-contents-switcher-{{switcher_name}}" data-label="toc-menu-mobile">
{% if request.path.endswith("table-of-contents") %}
<option selected disabled value="{{ url_for('table_of_contents', year=year, lang=lang) }}">{{ self.table_of_contents_title() }}</option>
<option disabled value="{{ url_for('table_of_contents', year=year, lang=lang) }}#foreword">{{ self.foreword_title() }}</option>
Expand Down Expand Up @@ -179,7 +179,7 @@
{% endif %}

{% if ebook_size_in_mb and ebook_size_in_mb > 0 %}
<option value="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf">
<option value="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf" data-event="ebook-click">
{{ self.ebook_download_short() }}
</option>
{% endif %}
Expand Down Expand Up @@ -275,7 +275,7 @@
<a href="{{ url_for('table_of_contents', year=year, lang=lang) }}#ebook">{{ self.ebook_title() }}</a>
</li>
<li class="nav-dropdown-list-chapter ebook">
<a href="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf">{{ self.ebook_download_short() }}</a>
<a href="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf" data-event="ebook-click" data-label="toc-menu">{{ self.ebook_download_short() }}</a>
</li>
{% endif %}
</ul>
Expand Down Expand Up @@ -482,10 +482,21 @@
element.hidden = false;
});

// Language and Year switching
// Language, Year and ToC switching
var languageYearSwitchers = document.querySelectorAll('.language-switcher select, .year-switcher select, .table-of-contents-switcher select');
for (var i = 0; i < languageYearSwitchers.length; i++) {
languageYearSwitchers[i].addEventListener('change', function(e) {

var selectedOption = this.options[this.selectedIndex];

if (e.target.dataset.label && selectedOption.dataset.event) {
gtag('event', selectedOption.dataset.event, {
'event_category': 'clicks',
'event_label': e.target.dataset.label,
'transport_type': 'beacon',
'value': 1 })
}

//Reset the selector back in case user uses Back button
var selectedValue = e.target.value;
if (selectedValue && selectedValue !== window.location.pathname) {
Expand Down Expand Up @@ -611,6 +622,17 @@
}
}
});

document.querySelectorAll('[data-event]').forEach(trackableElement => {
trackableElement.addEventListener('click', function (event) {
gtag('event', event.target.dataset.event, {
'event_category': 'clicks',
'event_label': event.target.dataset.label,
'transport_type': 'beacon',
'value': 1 })
});
});

})();
</script>
{% endblock %}
Expand Down
16 changes: 2 additions & 14 deletions src/templates/base/table_of_contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ <h2 id="appendices" class="part-name"><a href="#appendices" class="anchor-link">
<section class="part">
<h2 id="ebook" class="part-name"><a href="#ebook" class="anchor-link">{{ self.ebook_title() }}</a></h2>
<div class="chapters">
<div id="ebook-download" class="chapter">
<a href="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf">{{ self.ebook_download() }}</a><br>
<div class="chapter">
<a href="/static/pdfs/web_almanac_{{ year }}_{{ lang }}.pdf" data-event="ebook-click" data-label="toc-page">{{ self.ebook_download() }}</a><br>
<small>{{ self.ebook_download_note() }}</small>
</div>
</div>
Expand All @@ -163,15 +163,3 @@ <h2 id="ebook" class="part-name"><a href="#ebook" class="anchor-link">{{ self.eb
</div>
</main>
{% endblock %}


{% block scripts %}
{{ super() }}
<script nonce="{{ csp_nonce() }}">
var ebookLink = document.querySelector('#ebook-download');

ebookLink && ebookLink.addEventListener("click", function (event) {
gtag('event', 'ebook-click', { 'event_category': 'clicks', 'event_label': 'toc-page', 'value': 1 })
});
</script>
{% endblock %}