Saturday 29 April 2017

Cannot Authenticate Google Analytics Reporting API with Magic Mirror Module

I am working on a Magic Mirror module. This is my first time using node and i think i did some things wrong. But i cannot figure it out anymore with google and trying things out.

I have used the Hello Analytics template, and it worked when i started. But my code was just pasted below the node code and i knew this wasn't how it supposed to be. So i rewrote the code, but now it is not working anymore. I might have made a few mistakes, but i don't get any errors on my code in the javascript console.

I'll sum up what i did:

  • i created a project, and an API key for the module in the credentials app from google. i set everything up as asked by the hello analytics tutorial.
  • I added the ClientID to the meta tag in the head. I placed this directly in the index.html file. I also added the other required meta tag.
  • I created a view for my Analytics account and copied the view variable listed below. (the comment behind it is the Table id. as i didn't knew which one i needed.)

If the function queryReports is outside of the node framework, it works as intended and outputs data. But whenever i try to incorporate it in the node code it just throws an 401. Which means it is missing credentials to verify my user account. I have signed in with the OAuth2 button and it says i am singed in.

Can anyone tell me what i am doing wrong? Here is the code for the module:

/* global Module */

/* Magic Mirror
* Module: MMM-ga GoogleAnalytics module
*
* By Skippy Skefnietof http://www.skippyweb.nl
* MIT Licensed.
*/

Module.register("MMM-ga",{
// Default module config.
defaults: {
text: 'test text',
startDate: '2017-02-12',
endDate: 'today',
viewID: '37419435'//'ga:37419435'
},
  //getScripts: function() {
     //return ['https://apis.google.com/js/client:platform.js'];
  //},
getStyles: function() {
return ["MMM-ga.css"];
},
// Override dom generator.
getDom: function() {

var MMMga = document.createElement("div");
               MMMga.className = "MMM-ga" + this.config.text;

               var ga = document.createElement("p");
               ga.setAttribute('data-onsuccess','this.queryReports');
               ga.className = "g-signin2";
               MMMga.appendChild(ga);

               var gascript = document.createElement("script");
gascript.src = "https://apis.google.com/js/client:platform.js";
               MMMga.appendChild(gascript);

               var debugtxt = document.createElement("textarea");
               debugtxt.id = "query-output";
               MMMga.appendChild(debugtxt);
               return MMMga;
               //var queryReports = this.queryReports();
},
notificationReceived: function(notification, payload, sender) {
if (notification === "DOM_OBJECTS_CREATED") {
this.queryReports();
}
},
queryReports: function() {
console.log('!!!!!!!!LOOK AT ME I AM MR MEESEEKS! LOOK AT ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');

gapi.client.request({
     path: '/v4/reports:batchGet',
     root: 'https://analyticsreporting.googleapis.com/',
     method: 'POST',
     body: {
       reportRequests: [
         {
           viewId: this.config.viewID,
           dateRanges: [
             {
               startDate: '7daysAgo',
               endDate: 'today'
             }
           ],
           metrics: [
             {
               expression: 'ga:sessions'
             }
           ]
         }
       ]
     }
  }).then(this.displayResults, console.error.bind(console));
},
displayResults: function(response) {
var formattedJson = JSON.stringify(response.result, null, 2);
    document.getElementById('query-output').value = formattedJson;

}
});



via Skippengs

No comments:

Post a Comment