Skip to content

nubank/Amplitude-Flutter

 
 

Repository files navigation


amplitude_flutter

pub package

The Official Amplitude Flutter plugin. Used to track events with Amplitude.

Getting Started

To use this plugin, add amplitude_flutter as a dependency in your pubspec.yaml file.

Find the API key for your Amplitude project under your project settings.

Import package:amplitude_flutter/amplitude_flutter.dart, and instantiate AmplitudeFlutter with your API key.

In the example below - replace the string API_KEY with your API Key.

In addition, a Config object can be passed as a constructor argument for additional options. NOTE: This plugin's methods should only be called from the main isolate.

Advertising Id Tracking

In iOS, to enable Advertising Id tracking, you will need to add AdSupport.framework in your project setting page.

In Android, firstly you need to add com.google.android.gms:play-services-ads as a dependency in your build.gradle. If you use Google Mobile Ads SDK version 17.0.0 above. You need to add AD_MANAGER_APP into your androidmanifest.xml file.

Secondly, since we don't assume user's project will depend on this library, we use reflection to invoke its APIs. So the names of its classes can't be changed since reflection will use original name to find the class. You also need to add exception rules into your proguard-android.txt or proguard-rules.pro.

-keep class com.google.android.gms.ads.** { *; }

Adding Carrier Information

You can set an option in the config object titled getCarrierInfo to retrieve carrier name for a device. This is the ( Config). This object can be passed as a constructor argument for additional options.

If you set getCarrierInfo to true - recipients on Android devices will see a dialog box asking them for permission . This dialog will say allow app to make and manage phone calls. This is a message sent from the android operating system for the READ_PHONE_STATE permission and carrier info is grouped into this. If the user denies permission - carrier information will not be retrieved. The new android operating systems require asking a user for permission before retrieving this information.

By default the Config will set getCarrierInfo will default to false.

An example can be found here Example.

Example

import 'package:amplitude_flutter/amplitude_flutter.dart';

// replace 'API_KEY' with your project's API_KEY
Future<void> example() async {
  final AmplitudeFlutter analytics = AmplitudeFlutter('API KEY');

  // set this user's id
  analytics.setUserId('abc123');

  // log an event
  analytics.logEvent(name: 'add_friend');

  // Log events with event properties
  analytics.logEvent(name: 'add_friend', properties: { 'event_properties': { 'key': 'value' }});

  // identify a user
  final Identify identify = Identify()
    ..set('cohort', 'Test A')
    ..setOnce('completed_onboarding', 'true')
    ..add('login_count', 1)
    ..append('tags', 'new tag')
    ..unset('demo_user');

  analytics.identify(identify);

  // Amplitude Accounts [https://amplitude.zendesk.com/hc/en-us/articles/115001765532-Accounts] methods:
  // add a user to a group
  analytics.setGroup('orgId', 15);

  // change properties of a group
  analytics.groupIdentify('orgId', 15, Identify()..set('account_manager', 456));

  // emit an event associated with a group
  analytics.logEvent('Demo Released', properties: { 'groups': { 'orgId': 15 } });

  // Log revenue
  final Revenue revenue = Revenue()
    ..setPrice(23.23)
    ..setQuantity(3)
    ..setProductId('widget1')

  analytics.logRevenue(revenue);
}

Need Help?

If you have any problems or issues over our SDK, feel free to create a github issue or submit a request on Amplitude Help.

Packages

No packages published

Languages

  • Dart 70.9%
  • Objective-C 13.7%
  • Java 10.7%
  • Ruby 4.3%
  • Shell 0.4%