enableCookies external consent manager

Sorry I’m trying to understand.

Context:
cookie consent request requested through external service

piwik tracking code set to not save cookies

Once the consent was received, the idea was to pass the command

_paq.push (["enableCookies"]);

and in fact if after this command

_paq.push ([function () {
    console.log (this.hasCookies ());
}]);

gives me true

but cookies are not saved after this command

If I change the page
console.log (this.hasCookies ()); }]);
returns false
At this point I think we need to add a parameter in the tracking code, but I don’t know which one

Thank you

Hi there :slight_smile:

_paq.push(["enableCookies"]); does not create cookies. Those are created on the next tracking request.

There’s a special method that you should run after enabling cookies:

_paq.push(["deanonymizeUser"]);

Now it works, but I think the problem was the code I had used to install piwik

I had used

<script type="text/javascript">
(function(window, document, dataLayerName, id) {
window[dataLayerName]=window[dataLayerName]||[],window[dataLayerName].push({start:(new Date).getTime(),event:"stg.start"});var scripts=document.getElementsByTagName('script')[0],tags=document.createElement('script');
function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString()}document.cookie=a+"="+b+d+"; path=/"}
var isStgDebug=(window.location.href.match("stg_debug")||document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?14:-1);
var qP=[];dataLayerName!=="dataLayer"&&qP.push("data_layer_name="+dataLayerName),isStgDebug&&qP.push("stg_debug");var qPString=qP.length>0?("?"+qP.join("&")):"";
tags.async=!0,tags.src="https://xxxxx.containers.piwik.pro/"+id+".js"+qPString,scripts.parentNode.insertBefore(tags,scripts);
!function(a,n,i){a[n]=a[n]||{};for(var c=0;c<i.length;c++)!function(i){a[n][i]=a[n][i]||{},a[n][i].api=a[n][i].api||function(){var a=[].slice.call(arguments,0);"string"==typeof a[0]&&window[dataLayerName].push({event:n+"."+i+":"+a[0],parameters:[].slice.call(arguments,1)})}}(i[c])}(window,"ppms",["tm","cm"]);
})(window, document, 'dataLayer', 'xxx-xxx-xxxx-xxx');
</script>

Instead now using

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);
  (function() {
    var u="https://your-instance-name.piwik.pro/";
    _paq.push(["setTrackerUrl", u+"ppms.php"]);
    _paq.push(["setSiteId", "XXX-XXX-XXX-XXX-XXX"]);
    var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
    g.type="text/javascript"; g.async=true; g.defer=true; g.src=u+"ppms.js"; s.parentNode.insertBefore(g,s);
  })();
</script>

I have more control and can do what I described

No, that’s not the cause. Now you are using only the analytics module instead of the full stack (with Tag management and consent management). The Analytics code can be similarly customized via the product UI (Tag management → Tags → Piwik PRO).

yes. but in fact, as from the title of this post, I have an external consent manager and I need to manage via api if cookies are active or not

Sure. You can use Piwik PRO Tag Manager and disable built-in consent manager. To keep it convenient, you could disable the Piwik PRO tag and create a new, custom one, with the code you need.