Piwik call in AngularJS


#1

hello,

 I am integrating Piwik in my Mobile application made in Ionic using angularJS. I had integrated using angurlatics but for some scenarios it is not suitaable  for me. I have used the piwik.js directly in my Script tag 

My first question is how do i invoke the Piwik Tracker using AngularJS . Has anyone tried it before ? Since the piwik javascript is non angular.

I had used the below code in my angular controller, but i dont see any request sent to my piwik server. But if I use the Script tag which is provided by Piwik and similar to below working one , i see the request is sent. I am not sure what is stopping the request to be sent to the server using my javascript code.

Working-----------------

    <script src="libs/piwik/angulartics-piwik.js"></script>
	<script src="libs/piwik/piwik.js"></script>
    
	
	<script type="text/javascript">
        var _paq = _paq || [];
        _paq.push(["trackPageView"]);
        _paq.push(["enableLinkTracking"]);
          (function() {
            var u="//piwik-YYYYYY.YYYY.com/";
            _paq.push(['setTrackerUrl', u+'piwik.php']);
            _paq.push(['setSiteId', 1]);
          })();
     </script> 

NOT Working --------------------
/** Initialize Piwik **/

   var initPiwik = function(user){
       var _paq = _paq || [];
        _paq.push(["trackPageView"]);
        _paq.push(["enableLinkTracking"]);
        var u="//piwik-YYYYYYYY.YYYYYY.com/";
        _paq.push(['setTrackerUrl', u+'piwik.php']);
        _paq.push(['setSiteId', 1]);
        _paq.push(['setUserId', user]);

        //Have commented this since i have used the piwik js in my script tag
       //  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+'piwik.js'; s.parentNode.insertBefore(g,s);
         
    };

Can anyone provide any pointers?

regards
Felix


#2

I didn’t have a detailed look but most likely you do not want to remove the “var” in front of _paq


var initPiwik = function(user){ 
_paq = _paq || [];

If you write “var _paq” then “_paq” will be always undefined and will be defined only in that scope.


#3

You might also want to try angulartics, an angular library, which automatically calls the tracking code when needed for normal page views and, more importantly, virtual page views. The latter is lost when using the standard piwik tracking code.

https://luisfarzati.github.io/angulartics/


#4

Did you ever get this working? I’m trying the same…