androidlocationappmetrica

Get user country name with AppMetrica SDK in Android app


How to get user location (country name) via AppMetrica SDK (v7+) Java API in Android app?

I looking how to get user's country code within app with integrated AppMetrica SDK, without declaring location permissions. I'm sure that AppMetrica have algorithm to detect country (via IP or other ways), question how to get it from AppMetrica.


Solution

  • Per AppMetrica's documentation, you first need to enable location detection:

    <manifest>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <application>...</application>
    </manifest>
    

    Then, you can send the user's location using this snippet:

    // Creating an extended library configuration.
    AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY)
        // Enabling the data sending about the device location.
        .withLocationTracking(true)
        .build();
    // Initializing the AppMetrica SDK.
    AppMetrica.activate(getApplicationContext(), config);
    

    If you want to enable location tracking by default, you can do this:

    AppMetrica.setLocationTracking(true);
    

    This is all automatically done by AppMetrica. You can however, get the location manually by yourself and pass it along to Appmetrica. You can read more here.

    AppMetrica doesn't provide you with a way to get the user's country from inside it's SDK, but rather it only collects and sends the user's location. You can see the API reference here.