How to get visitoridcookie for plugin

Is there a function or method to get the current visitor’s idcookie in a plugin controller environment?

I have some custom data that is tied to the current visitor through the idcookie that I need to send back to the client on pageload and in order to retrieve it I need that string.

Here’s my current idea for a hack job.

Create a random number on the client side and send it to the backend as custom data. Store this as an additional field in the piwik_log_visit table. Drop this random number on the clients computer as a cookie for future access.

Load cookie, pass number to plugin, retrieve visitoridcookie and use that to send original custom data back to client for display.

I guess I could bypass everything with the idcookie and just use the random number to tie the visitor to the data but I hope there is a better way.

FYI, I will be working on putting ‘Visitor custom variable’ in Piwik in the next release. Keep an eye on the ticket: Custom Variables support: new JS API and new reports · Issue #1984 · matomo-org/matomo · GitHub or the next release.