Skip to content

Instantly share code, notes, and snippets.

@patridge
Created September 19, 2014 15:47
Show Gist options
  • Save patridge/8249de0abaa0f1e3678d to your computer and use it in GitHub Desktop.
Save patridge/8249de0abaa0f1e3678d to your computer and use it in GitHub Desktop.
Core reproduction code for `NoSuchMethodError` in Google Analytics code using Google Play Services Xamarin Component (v19.0.0.1)
using System;
using Android.App;
using Android.Content;
using Android.Gms.Analytics; // via Google Play Services Xamarin Component (v19.0.0.1)
using Android.Gms.Analytics.Ecommerce;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace GoogleAnalyticsIssueRepro
{
[Activity (Label = "GoogleAnalyticsIssueRepro", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
string someId = "UA-SomeId";
GoogleAnalytics googleAnalyticsInstance;
Tracker tracker;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
googleAnalyticsInstance = GoogleAnalytics.GetInstance(this);
tracker = googleAnalyticsInstance.NewTracker(someId);
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
tracker.SetScreenName("some title");
var screenViewBuilder1 = new HitBuilders.ScreenViewBuilder();
tracker.Send(screenViewBuilder1.Build()); // Works fine
var productAction = new ProductAction(ProductAction.ActionPurchase);
productAction.SetTransactionId("testorder123");
productAction.SetTransactionAffiliation("Some App");
productAction.SetTransactionRevenue((double)15.35m);
productAction.SetTransactionTax((double)3.3m);
productAction.SetTransactionShipping((double)6m);
var screenViewBuilder2 = new HitBuilders.ScreenViewBuilder();
// Attempt 1 (expected usage) => fail
screenViewBuilder2.SetProductAction(productAction); // fail
// Attempt 2 => fail
// screenViewBuilder2.SetAll(productAction.Build()); // fail
// Attempt 3 => fail
// foreach (var kvp in productAction.Build()) {
// screenViewBuilder2.Set(kvp.Key, kvp.Value); // fail
// }
tracker.Send(screenViewBuilder2.Build());
button.Text = string.Format ("{0} clicks!", count++);
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment