-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,140 @@ | ||
# appmetrica_sdk_example | ||
|
||
A new Flutter project. | ||
Demonstrates how to use the appmetrica_sdk plugin. | ||
|
||
## Getting Started | ||
### Example code | ||
|
||
This project is a starting point for a Flutter application. | ||
``` dart | ||
import 'dart:async'; | ||
A few resources to get you started if this is your first Flutter project: | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) | ||
import 'package:appmetrica_sdk/appmetrica_sdk.dart'; | ||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. | ||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
/// Initializing the AppMetrica SDK. | ||
await AppmetricaSdk() | ||
.activate(apiKey: 'db2206ed-c61a-43aa-b95c-6912f60bd25e'); | ||
runApp(MyApp()); | ||
} | ||
class MyApp extends StatefulWidget { | ||
@override | ||
_MyAppState createState() => _MyAppState(); | ||
} | ||
class _MyAppState extends State<MyApp> { | ||
String _libraryVersion = 'Unknown'; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
initLibrarayVersionState(); | ||
} | ||
// Platform messages are asynchronous, so we initialize in an async method. | ||
Future<void> initLibrarayVersionState() async { | ||
String libraryVersion; | ||
// Platform messages may fail, so we use a try/catch PlatformException. | ||
try { | ||
libraryVersion = await AppmetricaSdk().getLibraryVersion(); | ||
} on PlatformException { | ||
libraryVersion = 'Failed to get library version.'; | ||
} | ||
// If the widget was removed from the tree while the asynchronous platform | ||
// message was in flight, we want to discard the reply rather than calling | ||
// setState to update our non-existent appearance. | ||
if (!mounted) { | ||
return; | ||
} | ||
setState(() { | ||
_libraryVersion = libraryVersion; | ||
}); | ||
} | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Plugin example app'), | ||
), | ||
body: Container( | ||
padding: const EdgeInsets.all(8), | ||
child: Center( | ||
child: Column( | ||
children: <Widget>[ | ||
Container( | ||
child: Text( | ||
'AppMetrica SDK Library version: $_libraryVersion\n'), | ||
), | ||
RaisedButton( | ||
child: const Text('Send a custom event'), | ||
onPressed: () { | ||
/// Sending a custom event without nested parameters. | ||
AppmetricaSdk().reportEvent(name: 'Updates installed'); | ||
}, | ||
), | ||
RaisedButton( | ||
child: const Text( | ||
'Send a custom event with nested parameters'), | ||
onPressed: () { | ||
/// Sending a custom event with nested parameters. | ||
AppmetricaSdk().reportEvent( | ||
name: 'Current app statistics', | ||
attributes: <String, dynamic>{ | ||
'Application': 'com.company.myapp.awesomeapp', | ||
'Audience': 1000000000, | ||
'Product price in €': 10000000.99, | ||
'nested map': <String, dynamic>{ | ||
'strategies': 'Age of Empires', | ||
}, | ||
}); | ||
}, | ||
), | ||
RaisedButton( | ||
child: const Text('Send user profile attributes'), | ||
onPressed: () { | ||
/// Sending profile custom string attribute. | ||
AppmetricaSdk().reportUserProfileCustomString( | ||
key: 'string_attribute', value: 'string'); | ||
/// Sending profile custom number attribute. | ||
AppmetricaSdk().reportUserProfileCustomNumber( | ||
key: 'number_attribute', value: 55); | ||
/// Sending profile custom boolean attribute. | ||
AppmetricaSdk().reportUserProfileCustomBoolean( | ||
key: 'boolean_attribute', value: true); | ||
/// Sending profile custom attribute of the counter type. | ||
AppmetricaSdk().reportUserProfileCustomCounter( | ||
key: 'counter_attribute', delta: 1); | ||
/// Sets the ID of the user profile. Required for predefined | ||
/// profile attributes like Name or Notifications enabled. | ||
AppmetricaSdk().setUserProfileID(userProfileID: 'id'); | ||
/// Sending profile predefined user name attribute. | ||
AppmetricaSdk() | ||
.reportUserProfileUserName(userName: 'John'); | ||
/// Sending profile predefined NotificationsEnabled attribute. | ||
AppmetricaSdk().reportUserProfileNotificationsEnabled( | ||
notificationsEnabled: true); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
))); | ||
} | ||
} | ||
``` |