google-analytics-api
API di reporting (Analytics v4)
Ricerca…
Osservazioni
L' API di reporting di Google Analytics V4 è il metodo programmatico più avanzato per accedere ai dati dei rapporti in Google Analytics . Con l'API di reporting di Google Analytics, puoi:
- Crea dashboard personalizzati per visualizzare i dati di Google Analytics .
- Automatizza attività di reporting complesse per risparmiare tempo.
- Integra i tuoi dati di Google Analytics con altre applicazioni aziendali.
Caratteristiche
Google Analytics è basato su una potente infrastruttura di reporting dei dati. L'API di reporting di Google Analytics V4 ti consente di accedere alla potenza della piattaforma di Google Analytics . L'API fornisce queste funzionalità chiave:
- Espressioni metriche: l'API consente di richiedere non solo metriche incorporate ma anche combinazioni di metriche espresse in operazioni matematiche. Ad esempio, è possibile utilizzare l'espressione ga: goal1completions / ga: sessioni per richiedere i completamenti obiettivo per numero di sessioni.
- Più intervalli di date: l'API ti consente in un'unica richiesta di ottenere dati in due intervalli di date.
- Coorti e valore a vita: l'API ha un ricco vocabolario per richiedere rapporti sui valori Cohort e Lifetime.
- Più segmenti: l'API ti consente di ottenere più segmenti in una singola richiesta.
Esempio di report singolo con Oauth2 C #
Questo esempio utilizza la libreria ufficiale di Google .net Client.
Autorizzazione Richiede uno dei seguenti ambiti OAuth:
OAuth2
// These are the scopes of permissions you need.
string[] scopes = new string[] { AnalyticsReportingService.Scope.AnalyticsReadonly }; // View your Google Analytics Data
UserCredential credential;
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// Requesting Authentication or loading previously stored authentication for userName
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Reporting API service.
var service = new AnalyticsReportingService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = string.Format("{0} Authentication", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name),
});
Richiesta di segnalazione
// Create the DateRange object.
DateRange June2015 = new DateRange() { StartDate = "2015-01-01", EndDate = "2015-06-30" };
DateRange June2016 = new DateRange() { StartDate = "2016-01-01", EndDate = "2016-06-30" };
List<DateRange> dateRanges = new List<DateRange>() { June2016, June2015 };
// Create the ReportRequest object.
// This should have a large number of rows
ReportRequest reportRequest = new ReportRequest
{
ViewId = ConfigurationManager.AppSettings["GoogleAnaltyicsViewId"],
DateRanges = dateRanges,
Dimensions = new List<Dimension>() { new Dimension() { Name = "ga:date" }, new Dimension() { Name = "ga:usertype" } },
Metrics = new List<Metric>() { new Metric() { Expression= "ga:users" }, new Metric() { Expression = "ga:sessions" } },
PageSize = 1000,
};
List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);
var getReport = new GetReportsRequest() { ReportRequests = requests };
var response = service.Reports.BatchGet(getReport).Execute();
Esempio di rapporto singolo Rest
Le richieste API sono POST HTTP con il token di accesso collegato alla fine dell'estremità dell'API.
Autorizzazione Richiede uno dei seguenti ambiti OAuth:
Nota quando ContentType = "application/Json";
i dati usa ContentType = "application/Json";
https://analyticsreporting.googleapis.com/v4/reports:batchGet?Access_token={from auth}
{
"reportRequests":[
{
"viewId":"XXXX",
"dateRanges":[
{
"startDate":"2015-06-15",
"endDate":"2015-06-30"
}],
"metrics":[
{
"expression":"ga:sessions"
}],
"dimensions": [
{
"name":"ga:browser"
}]
}]
}