Matomo (Piwik) supports an opt out iframe, wich may supports your needs. But in some cases it is more handy to realize opt out / opt in via ajax requests and with no iframes. May due to security reasons.
I had such an project and implemented this nice small plugin wich offers you three new API URLs:
your.piwik/index.php?module=API&method=AjaxOptOut.isTrackedYou will get an Response whether the current user get's tracked or not.
your.piwik/index.php?module=API&method=AjaxOptOut.doIgnoreMatomo will set the ignore cookie for the current user.
your.piwik/index.php?module=API&method=AjaxOptOut.doTrackMatomo will remove the ignore cookie for the current user.
You have to use JSONP Requests, as of the AJAX requests needs to manipulate the cookies.