Google Analytics: Track PDF Downloads using Analytics.js

This blog is in continuation to existing one “Track PDF links on webpage” which was based on Google’s ga.js, the method which is now obsolete.

See GA Upgrade for more details.

Coming back to main objective here… we can still continue achieve of motive of tracking PDF clicks or downloads using below script:

(function(i, s, o, g, r, a, m) {
    i['GoogleAnalyticsObject'] = r;
    i[r] = i[r] || function() {
        (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();
    a = s.createElement(o), m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//', 'ga');
ga('create', 'UA-XXXXXXX-Y', 'auto');
ga('send', 'pageview');
$(document).ready(function() {
    $("a[href$='pdf']").each(function(index) {
        pdfLabel = $(this).attr('href');
        pdfOnClick = "ga('send', 'event', 'PDF', '" + pdfLabel + "');";
        $(this).attr("onClick", pdfOnClick);

Note: You need to include jQuery (mentioned below) to your page for leveraging this code in HEAD section of HTML.

<script type="text/javascript" src=""></script>
(Visited 264 times, 1 visits today)