flipboxfactory / craft-tracker
Track various actions such as page views and clicks
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
Open Issues: 0
Type:craft-plugin
Requires
- craftcms/cms: ^3.2
- flipboxfactory/craft-ember: ^2.1
Requires (Dev)
- codeception/codeception: ^3.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-11-05 00:10:45 UTC
README
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>