diff --git a/config/matomotracker.php b/config/matomotracker.php index f03ca7b..a458f16 100644 --- a/config/matomotracker.php +++ b/config/matomotracker.php @@ -20,5 +20,10 @@ /** * For queuing the tracking you can use custom queue names. Use 'default' if you want to run the queued items within the standard queue. */ - 'queue' => env('MATOMO_QUEUE', 'matomotracker') + 'queue' => env('MATOMO_QUEUE', 'matomotracker'), + + /** + * Optionally set a custom queue connection. Laravel defaults to "sync". + */ + 'queueConnection' => env('MATOMO_QUEUE_CONNECTION', 'default'), ]; diff --git a/readme.md b/readme.md index d63a6b2..b3389d3 100644 --- a/readme.md +++ b/readme.md @@ -39,6 +39,7 @@ MATOMO_URL="https://your.matomo-install.com" MATOMO_SITE_ID=1 MATOMO_AUTH_TOKEN="00112233445566778899aabbccddeeff" MATOMO_QUEUE="matomotracker" +MATOMO_QUEUE_CONNECTION="default" [...] ``` diff --git a/src/LaravelMatomoTracker.php b/src/LaravelMatomoTracker.php index 3ae10bb..94b359f 100644 --- a/src/LaravelMatomoTracker.php +++ b/src/LaravelMatomoTracker.php @@ -17,11 +17,14 @@ class LaravelMatomoTracker extends MatomoTracker protected $tokenAuth; /** @var string */ protected $queue; + /** @var string */ + protected $queueConnection; public function __construct(?Request $request, ?int $idSite = null, ?string $apiUrl = null, ?string $tokenAuth = null) { $this->tokenAuth = $tokenAuth ?: config('matomotracker.tokenAuth'); $this->queue = config('matomotracker.queue', 'matomotracker'); + $this->queueConnection = config('matomotracker.queueConnection', 'default'); $this->setTokenAuth(!is_null($tokenAuth) ? $tokenAuth : config('matomotracker.tokenAuth')); $this->setMatomoVariables($request, $idSite, $apiUrl); @@ -45,7 +48,8 @@ private function setMatomoVariables(Request $request, int $idSite = null, string $this->ecommerceItems = array(); $this->attributionInfo = false; $this->eventCustomVar = false; - $this->forcedDatetime = false; + // force-set time, so queued commands use the right request time + $this->forcedDatetime = time(); $this->forcedNewVisit = false; $this->networkTime = false; $this->serverTime = false; @@ -302,6 +306,7 @@ public function queuePageView(string $documentTitle) dispatch(function () use ($documentTitle) { $this->doTrackPageView($documentTitle); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -319,6 +324,7 @@ public function queueEvent(string $category, string $action, $name = false, $val dispatch(function () use ($category, $action, $name, $value) { $this->doTrackEvent($category, $action, $name, $value); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -335,6 +341,7 @@ public function queueContentImpression(string $contentName, string $contentPiece dispatch(function () use ($contentName, $contentPiece, $contentTarget) { $this->doTrackContentImpression($contentName, $contentPiece, $contentTarget); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -352,6 +359,7 @@ public function queueContentInteraction(string $interaction, string $contentName dispatch(function () use ($interaction, $contentName, $contentPiece, $contentTarget) { $this->doTrackContentInteraction($interaction, $contentName, $contentPiece, $contentTarget); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -368,6 +376,7 @@ public function queueSiteSearch(string $keyword, string $category = '', $countR dispatch(function () use ($keyword, $category, $countResults) { $this->doTrackSiteSearch($keyword, $category, $countResults); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -383,6 +392,7 @@ public function queueGoal($idGoal, $revencue = 0.0) dispatch(function () use ($idGoal, $revencue) { $this->doTrackGoal($idGoal, $revencue); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -397,6 +407,7 @@ public function queueDownload(string $actionUrl) dispatch(function () use ($actionUrl) { $this->doTrackDownload($actionUrl); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -411,6 +422,7 @@ public function queueOutlink(string $actionUrl) dispatch(function () use ($actionUrl) { $this->doTrackOutlink($actionUrl); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -425,6 +437,7 @@ public function queueEcommerceCartUpdate(float $grandTotal) dispatch(function () use ($grandTotal) { $this->doTrackEcommerceCartUpdate($grandTotal); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -464,6 +477,7 @@ public function queueEcommerceOrder( $discount ); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); } @@ -476,6 +490,7 @@ public function queueBulkTrack() dispatch(function () { $this->doBulkTrack(); }) + ->onConnection($this->queueConnection) ->onQueue($this->queue); }