将Google Analytics与xamarin安卓应用集成

时间:2017-05-11 12:17:12

标签: c# android visual-studio xamarin google-analytics

我正在使用Xamarin开发Android应用。我曾尝试按照谷歌的this教程要求我编辑build.gradle文件,但我的解决方案中没有此文件(也许是因为我在Xamarin中,我没有知道)。所以我搜索了另一个特别针对Xamarin用户的教程。 我找到了this一个,直到现在这些都是我的代码:

public class GAService
{
    public string TrackingId = "XX-XXXXXXXX-X";

    private static GoogleAnalytics GAInstance;
    private static Tracker GATracker;

    #region Instantiation ...
    private static GAService thisRef;
    private GAService()
    {

    }

    public static GAService GetGASInstance()
    {
        if (thisRef == null)

            thisRef = new GAService();
        return thisRef;
    }
    #endregion

    public void Initialize(Context AppContext)
    {
        GAInstance = GoogleAnalytics.GetInstance(AppContext.ApplicationContext);
        GAInstance.SetLocalDispatchPeriod(10);

        GATracker = GAInstance.NewTracker(TrackingId);
        GATracker.EnableExceptionReporting(true);
        GATracker.EnableAdvertisingIdCollection(true);
        GATracker.EnableAutoActivityTracking(true);
    }

    public void Track_App_Page(String PageNameToTrack)
    {
        GATracker.SetScreenName(PageNameToTrack);
        GATracker.Send(new HitBuilders.ScreenViewBuilder().Build());
    }

    public void Track_App_Event(String GAEventCategory, String EventToTrack)
    {
        HitBuilders.EventBuilder builder = new HitBuilders.EventBuilder();
        builder.SetCategory(GAEventCategory);
        builder.SetAction(EventToTrack);
        builder.SetLabel("AppEvent");

        GATracker.Send(builder.Build());
    }

    public void Track_App_Exception(String ExceptionMessageToTrack, Boolean isFatalException)
    {
        HitBuilders.ExceptionBuilder builder = new HitBuilders.ExceptionBuilder();
        builder.SetDescription(ExceptionMessageToTrack);
        builder.SetFatal(isFatalException);

        GATracker.Send(builder.Build());
    }
}

我在上面的代码中的三个地方遇到了同样的错误,第一个是:private static GoogleAnalytics GAInstance;表示The namescape 'GoogleAnalytics' could not be found。 第二个是:private static Tracker GATracker;The namescape 'Tracker' could not be found。 第三个是:HitBuilders.EventBuilder builder = new HitBuilders.EventBuilder();The namescape 'HitBuilders' could not be found。 我的问题的解决方案? 提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以查看this教程。特别是前两个点,在Android上,您需要安装Google Play Services Analytics NuGet包才能访问这些类。