mike-spainhower / angular-piwik

Angular service for piwik tracking
45 stars 20 forks source link

Not setting userid or custom variables #19

Open jdforsythe opened 9 years ago

jdforsythe commented 9 years ago

After the user logs in to the app I run:

Piwik.setUserId(user.id);

But nothing shows up in the piwik db for userid.

I've also tried:

Piwik.setCustomVariable('email', user.email);

Nothing shows up in the piwik db for that, either.

There is no error in the console and execution continues. Any ideas? The visits are being logged fine, but no user_id, custom_var_k1, or custom_var_k2

Here's the HTML:

<ngp-piwik
    ngp-set-js-url="https://analyze.mysite.com/piwik.js"
    ngp-set-tracker-url="https://analyze.mysite.com/piwik.php"
    ngp-set-site-id="1">
</ngp-piwik>

I'm loading the 'piwik' module into my angular app and injecting the 'Piwik' service into the LoginFactory.

SpainTrain commented 9 years ago

In the network tab of dev tools, can you see whether it is downloading the piwik.js file? If not, could you share some of the code that loads the module and injects the service? If it is loading the js file, do you see any communication with the piwik.php endpoint?

jdforsythe commented 9 years ago

It does load piwik.js and makes a single call to piwik.php

Load module:

var app = angular.module('myapp', ['piwik']);

Inject service:

(function() {
  function AuthFactory($http, $location, $sanitize, SessionFactory, Piwik) {
    var cacheSession = function(response) {
      SessionFactory.setUser(response.user);
      Piwik.setUserId(response.user.id);
      Piwik.setCustomVariable('email', response.user.email);
    };
    return {...};
  AuthFactory.$inject = ['$http', '$location', '$sanitize', 'SessionFactory', 'Piwik'];
  app.factory('AuthFactory', AuthFactory);
})();

If I console.log(Piwik) I get:

function Piwik()

If I console.log(Piwik.setUserId) [or setCustomVariable] I get:

function mod.factory._self.(anonymous function)()

I debugged the _self function. During the setUserId processing, right before return:

cmd = [
  "setUserId",
  3
];

and running $window['_paq'].push(cmd) returns undefined.

jdforsythe commented 9 years ago

Execution does not hang thereafter and I get no errors in the console

SpainTrain commented 9 years ago

Strange.

In the console, can you check what the value of window._paq is? Also, what happens if you manually execute window._paq.push('setUserId', 3)? If piwik is loaded, then manually pushing to that array should set the userid regardless of anything weird that may be happening with angular-piwik.

If we can get a reproduction of this bug running somewhere, I am happy to dig in and debug myself.

jdforsythe commented 9 years ago

The value of window._paq is:

Object {}
    push: R(){var W,Y,X;for(W=0;W<arguments.length;W+=1){X=arguments[W];Y=X.shift();if(n(Y)){L[Y].apply(L,X)}else{Y.apply(L,X)}}}
    __proto__: Object

I can manually execute window._paq(['setUserId', '3']) (note that this must be in an array and the value must be a string) or set a custom variable from the console, but in order for it to reach out to piwik.php, I must also execute window._paq.push(['trackPageView']). Doing either of these properly stores the values in the database and shows in the piwik pages.