Analytics
5.8K members online now
Understand information in your reports and troubleshoot reporting issues such as self-referrals, (not set) data, and inaccurate information
 
Guide Me
star_border
Reply

Can you use Reporting API V4 on HTTP website?

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi there,

I have the following code, taken from the example for the Reporting API (V4), however, when I try validating the data I get the following error:

 

400. That’s an error.

 

Error: origin_mismatch

 

Which I can only assume is because my website is on HTTP not HTTPS. However, I cannot move the site to HTTPS. So is there any way to get this to work?

 

The code for the website is:

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Hello Analytics Reporting API V4</title>
</head>
<body>

<button id="auth-button" hidden>Authorize</button>

<h1>Hello Analytics Reporting API V4</h1>

<textarea cols="80" rows="20" id="query-output"></textarea>

<script>

  // Replace with your client ID from the developer console.
  var CLIENT_ID = 'MY ID';

  // Replace with your view ID.
  var VIEW_ID = 'MY ID';


  // Set the discovery URL.
  var DISCOVERY = 'https://analyticsreporting.googleapis.com/$discovery/rest';

  // Set authorized scope.
  var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];


  function authorize(event) {
    // Handles the authorization flow.
    // `immediate` should be false when invoked from the button click.
    var useImmdiate = event ? false : true;
    var authData = {
      client_id: CLIENT_ID,
      scope: SCOPES,
      immediate: useImmdiate
    };

    gapi.auth.authorize(authData, function(response) {
      var authButton = document.getElementById('auth-button');
      if (response.error) {
        authButton.hidden = false;
      }
      else {
        authButton.hidden = true;
        queryReports();
      }
    });
  }

  function queryReports() {
    // Load the API from the client discovery URL.
    gapi.client.load(DISCOVERY
    ).then(function() {

        // Call the Analytics Reporting API V4 batchGet method.
        gapi.client.analyticsreporting.reports.batchGet( {
          "reportRequests":[
          {
            "viewId":VIEW_ID,
            "dateRanges":[
              {
                "startDate":"7daysAgo",
                "endDate":"today"
              }],
            "metrics":[
              {
                "expression":"ga:sessions"
              }]
          }]
        } ).then(function(response) {
          var formattedJson = JSON.stringify(response.result, null, 2);
          document.getElementById('query-output').value = formattedJson;
        })
        .then(null, function(err) {
            // Log any errors.
            console.log(err);
        });
    });
  }

  // Add an event listener to the 'auth-button'.
  document.getElementById('auth-button').addEventListener('click', authorize);
</script>

<script src="https://apis.google.com/js/client.js?onload=authorize"></script>

</body>
</html>

 

1 Expert replyverified_user

Re: Can you use Reporting API V4 on HTTP website?

Top Contributor
# 2
Top Contributor
Make sure you defined a return URL from your cloud console that uses http, not https?
VP & Chief Evangelist at Hub'Scan | Contact me
Level 80 Digital Analytics Warrior, KPI Therapist and Keeper of the One True Tagging Plan

Re: Can you use Reporting API V4 on HTTP website?

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
When I set all the https to http I get the following

400. That’s an error.

Error: invalid_scope

Some requested scopes were invalid. {invalid=[http://www.googleapis.com/auth/analytics.readonly]}

Learn more