firebase_analytics 9.0.0-dev.2 firebase_analytics: ^9.0.0-dev.2 copied to clipboard
Flutter plugin for Google Analytics for Firebase, an app measurement solution that provides insight on app usage and user engagement on Android and iOS.
// ignore_for_file: require_trailing_commas
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'tabs_page.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
appId: '1:448618578101:ios:2bc5c1fe2ec336f8ac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
));
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase Analytics Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(
title: 'Firebase Analytics Demo',
analytics: analytics,
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({
Key? key,
required this.title,
required this.analytics,
}) : super(key: key);
final String title;
final FirebaseAnalytics analytics;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _message = '';
void setMessage(String message) {
setState(() {
_message = message;
});
}
Future<void> _sendAnalyticsEvent() async {
await widget.analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
'string': 'string',
'int': 42,
'long': 12345678910,
'double': 42.0,
// Only strings and numbers (ints & doubles) are supported for GA custom event parameters:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets#overview
'bool': true.toString(),
},
);
setMessage('logEvent succeeded');
}
Future<void> _testSetUserId() async {
await widget.analytics.setUserId(id: 'some-user');
setMessage('setUserId succeeded');
}
Future<void> _testSetCurrentScreen() async {
await widget.analytics.setCurrentScreen(
screenName: 'Analytics Demo',
screenClassOverride: 'AnalyticsDemo',
);
setMessage('setCurrentScreen succeeded');
}
Future<void> _testSetAnalyticsCollectionEnabled() async {
await widget.analytics.setAnalyticsCollectionEnabled(false);
await widget.analytics.setAnalyticsCollectionEnabled(true);
setMessage('setAnalyticsCollectionEnabled succeeded');
}
Future<void> _testSetSessionTimeoutDuration() async {
await widget.analytics
.setSessionTimeoutDuration(const Duration(milliseconds: 20000));
setMessage('setSessionTimeoutDuration succeeded');
}
Future<void> _testSetUserProperty() async {
await widget.analytics.setUserProperty(name: 'regular', value: 'indeed');
setMessage('setUserProperty succeeded');
}
Item itemCreator() {
return Item(
affilitation: 'affil',
coupon: 'coup',
creative_name: 'creativeName',
creative_slot: 'creativeSlot',
discount: 'disc',
index: 3,
item_brand: 'itemBrand',
item_category: 'itemCategory',
item_category2: 'itemCategory2',
item_category3: 'itemCategory3',
item_category4: 'itemCategory4',
item_category5: 'itemCategory5',
item_id: 'itemId',
item_list_id: 'itemListId',
item_list_name: 'itemListName',
item_name: 'itemName',
item_variant: 'itemVariant',
location_id: 'locationId',
price: 'pri',
promotion_id: 'promotionId',
promotion_name: 'promotionName',
quantity: 'quantity',
);
}
Future<void> _testAllEventTypes() async {
await widget.analytics.logAddPaymentInfo();
await widget.analytics.logAddToCart(
currency: 'USD',
value: 123,
items: [itemCreator(), itemCreator()],
);
await widget.analytics.logAddToWishlist();
await widget.analytics.logAppOpen();
await widget.analytics.logBeginCheckout(
value: 123,
currency: 'USD',
items: [itemCreator(), itemCreator()],
);
await widget.analytics.logCampaignDetails(
source: 'source',
medium: 'medium',
campaign: 'campaign',
term: 'term',
content: 'content',
aclid: 'aclid',
cp1: 'cp1',
);
await widget.analytics.logEarnVirtualCurrency(
virtualCurrencyName: 'bitcoin',
value: 345.66,
);
await widget.analytics.logGenerateLead(
currency: 'USD',
value: 123.45,
);
await widget.analytics.logJoinGroup(
groupId: 'test group id',
);
await widget.analytics.logLevelUp(
level: 5,
character: 'witch doctor',
);
await widget.analytics.logLogin(loginMethod: 'sign up');
await widget.analytics.logPostScore(
score: 1000000,
level: 70,
character: 'tiefling cleric',
);
await widget.analytics
.logPurchase(currency: 'USD', transactionId: 'transaction-id');
await widget.analytics.logSearch(
searchTerm: 'hotel',
numberOfNights: 2,
numberOfRooms: 1,
numberOfPassengers: 3,
origin: 'test origin',
destination: 'test destination',
startDate: '2015-09-14',
endDate: '2015-09-16',
travelClass: 'test travel class',
);
await widget.analytics.logSelectContent(
contentType: 'test content type',
itemId: 'test item id',
);
await widget.analytics.logSelectPromotion(
creativeName: 'promotion name',
creativeSlot: 'promotion slot',
items: [itemCreator()],
locationId: 'United States');
await widget.analytics.logSelectItem(
items: [itemCreator(), itemCreator()],
itemListName: 't-shirt',
itemListId: '1234',
);
await widget.analytics.logScreenView(
screenName: 'tabs-page',
);
await widget.analytics.logViewCart(
currency: 'USD',
value: 123,
items: [itemCreator(), itemCreator()],
);
await widget.analytics.logShare(
contentType: 'test content type',
itemId: 'test item id',
method: 'facebook',
);
await widget.analytics.logSignUp(
signUpMethod: 'test sign up method',
);
await widget.analytics.logSpendVirtualCurrency(
itemName: 'test item name',
virtualCurrencyName: 'bitcoin',
value: 34,
);
await widget.analytics.logViewPromotion(
creativeName: 'promotion name',
creativeSlot: 'promotion slot',
items: [itemCreator()],
locationId: 'United States',
promotionId: '1234',
promotionName: 'big sale',
);
await widget.analytics.logRefund(
currency: 'USD',
value: 123,
items: [itemCreator(), itemCreator()],
);
await widget.analytics.logTutorialBegin();
await widget.analytics.logTutorialComplete();
await widget.analytics.logUnlockAchievement(id: 'all Firebase API covered');
await widget.analytics.logViewItem(
currency: 'usd',
value: 1000,
items: [itemCreator()],
);
await widget.analytics.logViewItemList(
itemListId: 't-shirt-4321',
itemListName: 'green t-shirt',
items: [itemCreator()]);
await widget.analytics.logViewSearchResults(
searchTerm: 'test search term',
);
setMessage('All standard events logged successfully');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
MaterialButton(
onPressed: _sendAnalyticsEvent,
child: const Text('Test logEvent'),
),
MaterialButton(
onPressed: _testAllEventTypes,
child: const Text('Test standard event types'),
),
MaterialButton(
onPressed: _testSetUserId,
child: const Text('Test setUserId'),
),
MaterialButton(
onPressed: _testSetCurrentScreen,
child: const Text('Test setCurrentScreen'),
),
MaterialButton(
onPressed: _testSetAnalyticsCollectionEnabled,
child: const Text('Test setAnalyticsCollectionEnabled'),
),
MaterialButton(
onPressed: _testSetSessionTimeoutDuration,
child: const Text('Test setSessionTimeoutDuration'),
),
MaterialButton(
onPressed: _testSetUserProperty,
child: const Text('Test setUserProperty'),
),
Text(_message,
style: const TextStyle(color: Color.fromARGB(255, 0, 155, 0))),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute<TabsPage>(
settings: const RouteSettings(name: TabsPage.routeName),
builder: (BuildContext context) {
return const TabsPage();
}));
},
child: const Icon(Icons.tab),
),
);
}
}