Skip to content

Developer events

Download Tracker fires an event just before it records a download, so a custom module or plugin can inspect the download, decide whether it should count, or react to it. This is the hook to reach for when the built-in settings don't cover a rule you need.

beforeTrackDownload

The Downloads service fires Downloads::EVENT_BEFORE_TRACK_DOWNLOAD before a download is counted. The event is a coyshdigital\downloadtracker\events\DownloadEvent, with these properties:

PropertyTypeWhat it is
downloadKeystringThe file's canonical identity (asset:123, path:..., url:...).
assetId?intThe related asset ID, when the download resolves to one.
asset?AssetThe related asset, when resolvable.
sourceTypestringOne of asset, path, url.
filenamestringThe file's display name.
isCrawlerboolWhether the hit came from a crawler rather than a person.
isValidboolWhether the download should be counted. Set to false to skip it.

Set isValid to false to stop the download being recorded. Everything else is read-only context to base that decision on.

Example: skip downloads of a particular file

php
use coyshdigital\downloadtracker\events\DownloadEvent;
use coyshdigital\downloadtracker\services\Downloads;
use yii\base\Event;

Event::on(
    Downloads::class,
    Downloads::EVENT_BEFORE_TRACK_DOWNLOAD,
    function (DownloadEvent $event) {
        // Don't count downloads of internal test files.
        if (str_starts_with($event->filename, '_internal-')) {
            $event->isValid = false;
        }
    }
);

Put this in the init() method of a custom module or plugin.

Example: make your own crawler decision

Because the event carries isCrawler, a handler can decide about crawler hits itself rather than relying on the Crawler downloads setting. For example, to count crawler hits everywhere except one noisy path:

php
Event::on(
    Downloads::class,
    Downloads::EVENT_BEFORE_TRACK_DOWNLOAD,
    function (DownloadEvent $event) {
        if ($event->isCrawler && str_contains($event->downloadKey, '/press/')) {
            $event->isValid = false;
        }
    }
);

This runs alongside the setting, so it's a way to add a narrow exception without turning off crawler counting for the whole site. For the built-in options, see Crawlers.

Digital help you can trust.