Resubmit events to an external service

Hi,

I am already using Piwik to track events of my app. Now, I would like to catch these events from Piwik, and send a request to an external service. I have started to develop a new plugin, but I cannot successfully subscribe it to the “actions.events.saved” hook.

From the documentation and the code , it seems the “Tracker.recordAction” (\piwik\core\Tracker\Action.php line 414) event is deprecated and dimensions (Dimensions: Develop - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3) are the way to go. However, I do not want to generate new tracking data, I just want to send a new http request to an external service when Piwik stores an event. How could I subscribe to this?

Any help or example would be really appreciated.

Best regards

Hi,

As I have not managed to use dimensions, I have used the Tracker.recordAction hook.

This is the code of my plugin:

public function registerEvents()
{
    return array(
        'Tracker.recordAction' => array(
            'after' => true,
            'function' => 'submitEvent'
        )
    );
}
public function submitEvent($trackerAction, $visitAction)
{
    $idUser        = bin2hex($visitAction['idvisitor']);
    $idVisit       = $visitAction['idvisit'];
    $eventName     = $trackerAction->getEventName();
    $eventCategory = $trackerAction->getEventCategory();
    $eventAction   = $trackerAction->getEventAction();
   
    $cd1 = $visitAction['custom_dimension_1'];
.....

}
}

1 Like

I couldn’t get your code to work with Piwik 3.x, there was talk on the github about the Tracker.recordAction event being deprecated in favour of Dimensions.

Here’s what I ended up writing:

class ExternalTriggerActionDimension extends ActionDimension
{
	
    /**
     * This event is triggered before a new action is logged to the log_link_visit_action table. It overwrites any
     * looked up action so it makes usually no sense to implement both methods but it sometimes does. You can assign
     * any value to the column or return boolan false in case you do not want to save any value.
     *
     * @param Request $request
     * @param Visitor $visitor
     * @param Action $action
     *
     * @return mixed|false
     */
    public function onNewAction(Request $request, Visitor $visitor, Action $action) {
    	if ($action instanceof ActionEvent) {
    		// We only care about events
    		$eventName     = $action->getEventName();
			$eventCategory = $action->getEventCategory();
			$eventAction   = $action->getEventAction();
			
			$json = json_encode(array(
				'event' => array(
					'eventName' => $eventName,
					'eventCategory' => $eventCategory,
					'eventAction' => $eventAction,
				)
			));
			
			$this->async_request("http://example.com/trigger_event", $json);
    	}
    	
    	return false; // Always return false, otherwise database fails
    }
    
    private function async_request($url, $payload) {
    	// Creates a server command to curl to the url, this means PHP can continue running and leave CURL to do its thing
		$cmd = "curl -X POST -H 'Content-Type: application/json' -d '" . $payload . "' " . "'" . $url . "' > /dev/null 2>&1 &";
		exec($cmd, $output, $exit);
		return $exit == 0;
	}

}

Sharing in the hope it might help someone who searches like I did and this thread comes up

Thanks Mitchell

I will try it next time I use Piwik, nowadays I still have not migrated
to Piwik 3.X