There is a problem, I can't pull the data and the chrt.js graph does not appear, what is the reason? I have this error on console but my id and API key is correct there is nothing wrong. Can someone help?
Not a valid origin for the client: https://www.example.com has not been registered for client ID.
There is a console error:
Authentication failed idpiframe_initialization_failed
Here is my code:
function initGoogleAnalytics() {
gapi.client.init({
'apiKey': 'my_api_key',
'clientId': 'my_client_id',
'scope': 'https://www.googleapis.com/auth/analytics.readonly',
'discoveryDocs': ['https://analyticsreporting.googleapis.com/$discovery/rest?version=v4']
}).then(function() {
fetchAnalyticsData();
}, function(error) {
console.log('Authentication failed: ' + error.error);
});
}
function fetchAnalyticsData() {
gapi.client.analyticsreporting.reports.batchGet({
'reportRequests': [{
'viewId': 'my_view_id',
'dateRanges': [{
'startDate': '2023-01-01',
'endDate': '2023-12-31'
}],
'metrics': [{
'expression': 'ga:sessions'
}]
}]
}).then(function(response) {
var report = response.result.reports[0];
var dataRows = report.data.rows;
var labels = [];
var sessionsData = [];
dataRows.forEach(function(row) {
labels.push(row.dimensions[0]);
sessionsData.push(parseInt(row.metrics[0].values[0]));
});
var ctx = document.getElementById('CharTwo').getContext('2d');
var CharTwo = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Number of Visitors',
data: sessionsData,
fill: false,
borderColor: 'rgba(75, 192, 192, 1)',
tension: 0.1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
max: 1000
}
}
}
});
}, function(error) {
console.log('An error occurred while retrieving Google Analytics data: ' + error.result.error.message);
});
}
function handleClientLoad() {
gapi.load('client:auth2', initGoogleAnalytics);
}```