【发布时间】:2018-10-09 09:16:45
【问题描述】:
我正在尝试从 Multi-Channel Funnels Reporting API 中检索第一次/最后一次/辅助互动。我似乎无法弄清楚如何在 C# 中连接到 API。 我已经与 Core Reporting API 建立了有效的连接。任何帮助/工作示例将不胜感激。
更新 --> 工作代码(对我来说)
public class MCFHelper
{
/// <summary>
/// Returns Analytics Multi-Channel Funnels data for a view (profile).
/// Documentation https://developers.google.com/analytics/v3/reference/mcf/get
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong.
/// </summary>
/// <param name="service">Authenticated Analytics service.</param>
/// <param name="ids">Unique table ID for retrieving Analytics data. Table ID is of the form ga:XXXX, where XXXX is the Analytics view (profile) ID.</param>
/// <param name="start-date">Start date for fetching Analytics data. Requests can specify a start date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or 7daysAgo). The default value is 7daysAgo.</param>
/// <param name="end-date">End date for fetching Analytics data. Requests can specify a start date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or 7daysAgo). The default value is 7daysAgo.</param>
/// <param name="metrics">A comma-separated list of Multi-Channel Funnels metrics. E.g., 'mcf:totalConversions,mcf:totalConversionValue'. At least one metric must be specified.</param>
/// <param name="optional">Optional paramaters.</param>
/// <returns>McfDataResponse</returns>
public static McfData Get(AnalyticsService service, string ids, string startdate, string enddate, string metrics, McfGetOptionalParms optional = null)
{
try
{
// Initial validation.
if (service == null)
throw new ArgumentNullException("service");
if (ids == null)
throw new ArgumentNullException(ids);
if (startdate == null)
throw new ArgumentNullException(startdate);
if (enddate == null)
throw new ArgumentNullException(enddate);
if (metrics == null)
throw new ArgumentNullException(metrics);
// Building the initial request.
var request = service.Data.Mcf.Get(ids, startdate, enddate, metrics);
// Applying optional parameters to the request.
request = (McfResource.GetRequest)SampleHelpers.ApplyOptionalParms(request, optional);
// Requesting data.
var result = request.Execute();
return result;
}
catch (Exception ex)
{
throw new Exception("Request Mcf.Get failed.", ex);
}
}
public static McfData ImportMcfData(string accessToken, GoogleAnalyticsApi.GoogleAnalyticsClientApi client)
{
MCFHelper.McfSample.McfGetOptionalParms dimensions = new MCFHelper.McfSample.McfGetOptionalParms();
dimensions.Dimensions = ""; <-- insert dimensions
var metrics = ""; <-- insert metrics
var service = new AnalyticsService(new BaseClientService.Initializer
{
ApplicationName = "",
HttpClientInitializer = new CustomUserCredential(accessToken)
});
string ids = "ga:"; <-- insert viewId
var data = MCFHelper.Get(service, ids, "2018-10-15", "today", metrics, dimensions);
return data;
}
public static class McfSample
{
public class McfGetOptionalParms
{
/// A comma-separated list of Multi-Channel Funnels dimensions. E.g., 'mcf:source,mcf:medium'.
public string Dimensions { get; set; }
/// A comma-separated list of dimension or metric filters to be applied to the Analytics data.
public string Filters { get; set; }
/// The maximum number of entries to include in this feed.
public string Metrics { get; set; }
}
}
public static class SampleHelpers
{
/// <summary>
/// Using reflection to apply optional parameters to the request.
///
/// If the optonal parameters are null then we will just return the request as is.
/// </summary>
/// <param name="request">The request. </param>
/// <param name="optional">The optional parameters. </param>
/// <returns></returns>
public static object ApplyOptionalParms(object request, object optional)
{
if (optional == null)
return request;
System.Reflection.PropertyInfo[] optionalProperties = (optional.GetType()).GetProperties();
foreach (System.Reflection.PropertyInfo property in optionalProperties)
{
// Copy value from optional parms to the request. They should have the same names and datatypes.
System.Reflection.PropertyInfo piShared = (request.GetType()).GetProperty(property.Name);
if (property.GetValue(optional, null) != null) // TODO Test that we do not add values for items that are null
piShared.SetValue(request, property.GetValue(optional, null), null);
}
return request;
}
}
}
块引用
【问题讨论】:
-
请编辑您的问题并包含您当前使用的代码。您知道核心报告 api 与 Multi channel funneling api 不同
标签: c# google-analytics google-api google-analytics-api google-api-dotnet-client