When tracking usage statistics and crashes with Visual Studio App Center, you usually only want real-world data in there and filter out data that gets generated while developing or testing the app. To avoid sophisticated insights like those in the screenshot below, developers need to define, when Visual Studio App Center should track data and when it should not.

Analytics and crash data usually should not impact those statistics, when an app is

  • Running in Debug Mode
  • Running on a Simulator or Emulator
  • Running in the Xamarin Test Cloud

Before starting the client analytics using the AppCenter.Start() method, we should scan the current environment to decide whether to run this command or not. For this, we implement a method on each platform, that scans if the current app environment is a real-world scenario.

You might want to check if the app is running on a Test Cloud device to avoid messed up UI Test from the Distribution Dialog and Denmark (where the Test Cloud devices live) as you most popular country in the statistics!

The App Center Test Cloud(formerly known as Xamarin Test Cloud) sets specificenvironment variables on its devices, that we can examineand use to check if the app is running on a test device.

Abstraction

Creating an interface for the small Environment Service helps to unify its implementations and usage within Dependency Injection.

public interface IEnvironmentService { bool IsRunningInRealWorld(); }

iOS implementation

public class EnvironmentService : IEnvironmentService { public bool IsRunningInRealWorld() { // Check if running in Debug Mode #if DEBUG return false; #endif // Check if running on a Simulator if (Runtime.Arch == Arch.SIMULATOR) return false; // Check if running in the Test Cloud if (Environment.GetEnvironmentVariable("XAMARIN_TEST_CLOUD") != null) return false; return true; } }

Android implementation

public class EnvironmentService : IEnvironmentService { public bool IsRunningInRealWorld() { #if DEBUG return false; #endif // Check if running on an Emulator if (Build.Fingerprint.Contains("vbox") || Build.Fingerprint.Contains("generic")) return false; // Check if running in the Test Cloud if (System.Environment.GetEnvironmentVariable("XAMARIN_TEST_CLOUD") != null) return false; return true; } }

Add the real-world check before starting to track analytics

Before starting the App Center service now, we can simply fire anEnvironmentService.IsRunningInRealWorld() check and only proceed if it is positive.

// Only start Visual Studio App Center when running in a real-world scenario var environment = new EnvironmentService(); if (environment.IsRunningInRealWorld()) { AppCenter.Start("YOUR_SECRET", typeof(Analytics), typeof(Crashes)); }

Xamarin.Forms

If you are using Xamarin.Forms, just register the platform implementations at thebuilt-in Dependency Service.

[assembly: Xamarin.Forms.Dependency(typeof(EnvironmentService))] namespace Something { public class EnvironmentService : IEnvironmentService { // ... } }

Now you can call the environment service in the App.xaml.cs like below.

var environment = DependencyService.Get();

This should avoid havingsophisticated Development and Testing data in you Visual Studio Mobile Center Analytics and Crashes section.

Enjoy the cleaned up dashboards!