flipboxfactory/craft-tracker

Track various actions such as page views and clicks

dev-master / 1.2.x-dev 2021-03-04 16:22 UTC

This package is auto-updated.

Last update: 2024-11-05 00:10:45 UTC


README

Join the chat at https://gitter.im/flipboxfactory/craft-tracker Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Installation

To install, use composer:

composer require flipboxfactory/craft-tracker

Testing

$ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

Please see License File for more information.

Examples

List Event Counts via RollUp object

<ul>
{% for rollUp in craft.tracker.rollUp.author(currentUser).all()
%}
    <li>
        <h3>{{ rollUp.entry.title }}</h3>
        Hit Count: {{ rollUp.count }}
    </li>
{% endfor %}
</ul>

List All Events By Entry

{% set entryId = 1 %}
<ul>
{% for event in craft.tracker.query.entryId(entryId).all() %}
    <li>
        <h3>{{ event.title }} -- {{ event.event }}</h3>
        <strong>User Agent:</strong> {{ event.userAgent }} <br />
        <strong>IP:</strong> {{ event.remoteIp }} <br />
        <strong>OS:</strong> {{ event.clientOs }} <br />
        <string>Created At:</string> {{ event.dateCreated }} <br />
        <strong>Metadata:</strong>
        <ul>
            {% for key, value in event.metadata|json_decode %}
                <li>
                    <strong>{{ key }}</strong> - {{ value }}
                </li>
            {% endfor %}
        </ul>
    </li>
{% endfor %}
</ul>

Record Event via TWIG Template

{# Basic 'Page View' type tracking #}
{% do craft.tracker.track({entry: entry}) %}

{# Explicitly set track attributes #}
{% do craft.tracker.track({entry: entry, event: "Viewed::PDF"}) %}

Record Event (AJAX / JQuery)

{% set metadata = {foo: "bar"} %}

<a href="https://google.com"
   data-event="Click::LinkToGoogle"
   data-entry-id="131"
   data-element-id="125"
   data-metadata="{{ metadata|json_encode() }}"
   data-title="Lorem ipsum dolor sit amet, consectetur adipiscing elit">Goto Google</a>


<script src="https://code.jquery.com/jquery-3.5.1.min.js"
        integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

<script>
    $(function () {
        $('[data-event][data-entry-id]').click(function () {
            let data = $(this).data();
            data.{{ craft.app.config.general.csrfTokenName }} = "{{ craft.app.request.csrfToken }}"

            console.log("Begin track event", data);

            $.ajax({
                async: false,
                type: "POST",
                url: "{{ actionUrl('/tracker/track/event') }}",
                data: JSON.stringify(data),
                headers: {
                    Accept: "application/json; charset=utf-8",
                    "Content-Type": "application/json; charset=utf-8"
                },
                contentType: "application/json",
                dataType: "json",
                success: function (response) {
                    console.log("End track event", response);
                },
            });
        });
    });
</script>