From 7de68000bcf1bdaf9e59028cea2b867066ee9a69 Mon Sep 17 00:00:00 2001 From: Andrew Belousoff Date: Tue, 16 Feb 2021 18:26:57 +0300 Subject: [PATCH] example. restore readme --- example/README.md | 142 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 9 deletions(-) diff --git a/example/README.md b/example/README.md index 77729f5..88d69da 100755 --- a/example/README.md +++ b/example/README.md @@ -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 { + String _libraryVersion = 'Unknown'; + + @override + void initState() { + super.initState(); + initLibrarayVersionState(); + } + + // Platform messages are asynchronous, so we initialize in an async method. + Future 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: [ + 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: { + 'Application': 'com.company.myapp.awesomeapp', + 'Audience': 1000000000, + 'Product price in €': 10000000.99, + 'nested map': { + '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); + }, + ), + ], + ), + ), + ))); + } +} + +```