Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Update example app #172

Merged
merged 13 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ This is the official Amplitude Flutter SDK developed and maintained by Amplitude

## Compatibility

From Amplitude Flutter v3.11.0, we bump up the kotlin version to v1.7.10 to support latest Gradle. For Gradle Version lower than v6.7.1, we recommend to use the Amplitude Flutter v3.10.0. The following matrix lists the minimum support for Amplitude Flutter SDK version.
From Amplitude Flutter v4, we bump up the kotlin version to v1.9.22 to support latest Gradle.

|Amplitude Flutter|Gradle|Android Gradle Plugin|Kotlin Gradle Plugin|
|-|-|-|-|
| `3.11.+` | `6.7.1` | `3.6.4` | `1.7.10` |
The following matrix lists the minimum support for Amplitude Flutter SDK version.

| Amplitude Flutter | Gradle | Android Gradle Plugin | Kotlin Gradle Plugin |
|-------------------|-------|-----------------------|----------------------|
| `4.+` | `8.2` | `8.2.2` | `1.9.22` |


Learn more about the Android [Gradle Plugin compatibility](https://developer.android.com/studio/releases/gradle-plugin#updating-gradle), [Gradle compatibility](https://docs.gradle.org/current/userguide/compatibility.html#kotlin), and [Kotlin compatibility](https://kotlinlang.org/docs/whatsnew17.html#bumping-minimum-supported-versions).
Expand Down
10 changes: 9 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group 'com.amplitude.amplitude_flutter'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.5.20'
ext.kotlin_version = '1.9.22'
repositories {
google()
mavenCentral()
Expand Down Expand Up @@ -61,3 +61,11 @@ tasks.withType(Test) {
showStandardStreams = true
}
}

kotlin {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ samples, guidance on mobile development, and a full API reference.
## Run the example
Assuming you have Flutter setup on your machine.

Update your Amplitude API key in `lib/main.dart`.

### Android & iOS
Open the emulator you want to test on (Android, iOS)
```shell
Expand Down
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
namespace 'com.example.amplitude_flutter_example'
compileSdkVersion 34

sourceSets {
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amplitude_flutter_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
9 changes: 5 additions & 4 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amplitude_flutter_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="${applicationName}"

android:label="amplitude_flutter_example"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true">

<meta-data
android:name="flutterEmbedding"
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/profile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amplitude_flutter_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.6.21'
ext.kotlin_version = '1.9.22'
repositories {
google()
mavenCentral()
Expand Down
3 changes: 1 addition & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sat Sep 25 00:36:28 PDT 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.2-bin.zip
34 changes: 34 additions & 0 deletions example/lib/device_id_form.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import 'app_state.dart';

class DeviceIdForm extends StatefulWidget {
@override
_DeviceIdFormState createState() => _DeviceIdFormState();
}

class _DeviceIdFormState extends State<DeviceIdForm> {
void Function(String) makeHandler(BuildContext context) {
return (String deviceId) {
AppState
.of(context)
.analytics
..setDeviceId(deviceId);
};
}

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Device Id', style: Theme.of(context).textTheme.headlineSmall),
const SizedBox(height: 10),
new TextField(
autocorrect: false,
decoration: InputDecoration(labelText: 'Device Id'),
onChanged: makeHandler(context),
),
],
);
}
}
45 changes: 0 additions & 45 deletions example/lib/deviceid_sessionid.dart

This file was deleted.

3 changes: 2 additions & 1 deletion example/lib/event_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:amplitude_flutter/events/base_event.dart';

import 'app_state.dart';

Expand All @@ -13,7 +14,7 @@ class _EventFormState extends State<EventForm> {

void onPress() {
AppState.of(context)
// ..analytics.logEvent(_controller.text)
..analytics.track(event: BaseEvent(eventType: _controller.text))
..setMessage('Event sent.');
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/group_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class _GroupFormState extends State<GroupForm> {
void onPress() {
if (groupType.text.isNotEmpty && groupValue.text.isNotEmpty) {
AppState.of(context)
// ..analytics.setGroup(groupType.text, groupValue.text)
..analytics.setGroup(groupType: groupType.text, groupName: groupValue.text)
..setMessage('Group set.');
}
}
Expand Down
8 changes: 4 additions & 4 deletions example/lib/group_identify_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import 'package:amplitude_flutter/identify.dart';
import 'package:amplitude_flutter/events/identify.dart';
import 'package:flutter/material.dart';

import 'app_state.dart';
Expand All @@ -14,11 +14,11 @@ class _GroupIdentifyFormState extends State<GroupIdentifyForm> {
groupValue.text.isNotEmpty &&
groupPropertyKey.text.isNotEmpty &&
groupPropertyValue.text.isNotEmpty) {
// final Identify identify = Identify()
// ..set(groupPropertyKey.text, groupPropertyValue.text);
final Identify identify = Identify()
..set(property: groupPropertyKey.text, value: groupPropertyValue.text);

AppState.of(context)
// ..analytics.groupIdentify(groupType.text, groupValue.text, identify)
..analytics.groupIdentify(groupType: groupType.text, groupName: groupValue.text, identify: identify)
..setMessage('Group Identify sent.');
}
}
Expand Down
22 changes: 14 additions & 8 deletions example/lib/identify_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:amplitude_flutter/identify.dart';
import 'package:amplitude_flutter/events/identify.dart';
import 'package:flutter/material.dart';

import 'app_state.dart';
Expand All @@ -11,16 +11,17 @@ class IdentifyForm extends StatefulWidget {
class _IdentifyFormState extends State<IdentifyForm> {
void onPress() {
final Identify identify = Identify()
..set('identify_test',
'identify sent at ${DateTime.now().millisecondsSinceEpoch}')
..add('identify_count', 1);
..set(property: 'identify_test', value: 'identify sent at ${DateTime
.now()
.millisecondsSinceEpoch}')
..add(property: "identify_count", value: 1);

if (userPropKey.isNotEmpty && userPropValue.isNotEmpty) {
identify.set(userPropKey, userPropValue);
identify.set(property: userPropKey, value: userPropValue);
}

AppState.of(context)
// ..analytics.identify(identify)
..analytics.identify(identify: identify)
..setMessage('Identify sent.');
}

Expand All @@ -30,12 +31,17 @@ class _IdentifyFormState extends State<IdentifyForm> {
@override
Widget build(BuildContext context) {
final InputDecoration dec = InputDecoration()
..applyDefaults(Theme.of(context).inputDecorationTheme);
..applyDefaults(Theme
.of(context)
.inputDecorationTheme);

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Identify', style: Theme.of(context).textTheme.headlineSmall),
Text('Identify', style: Theme
.of(context)
.textTheme
.headlineSmall),
const SizedBox(height: 10),
Row(children: <Widget>[
Expanded(
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import 'package:flutter/material.dart';

import 'my_app.dart';

void main() => runApp(const MyApp('a79cefed0b7076cf3998ef7578a18bf0'));
void main() => runApp(const MyApp('API_KEY'));
54 changes: 19 additions & 35 deletions example/lib/my_app.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import 'dart:async';

import 'package:amplitude_flutter/amplitude.dart';
import 'package:amplitude_flutter_example/flush_thresholds_form.dart';
import 'package:amplitude_flutter/configuration.dart';
import 'package:amplitude_flutter/constants.dart';
import 'package:flutter/material.dart';

import 'app_state.dart';
import 'deviceid_sessionid.dart';
import 'device_id_form.dart';
import 'event_form.dart';
import 'group_form.dart';
import 'group_identify_form.dart';
import 'identify_form.dart';
import 'regenerate_device.dart';
import 'reset.dart';
import 'revenue_form.dart';
import 'user_id_form.dart';

Expand All @@ -26,41 +27,24 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
String _message = '';

late final Amplitude analytics;
late Amplitude analytics;

initAnalytics() async {
await analytics.init();

setMessage('Amplitude initialized');
}

@override
void initState() {
super.initState();

// analytics = Amplitude.getInstance(instanceName: "project");
// analytics.setUseDynamicConfig(true);
// analytics.setServerUrl("https://api2.amplitude.com");
// analytics.init(widget.apiKey);
// analytics.enableCoppaControl();
// analytics.setUserId("test_user", startNewSession: true);
// analytics.trackingSessionEvents(true);
// analytics.setMinTimeBetweenSessionsMillis(5000);
// analytics.setEventUploadThreshold(5);
// analytics.setEventUploadPeriodMillis(30000);
// analytics.setServerZone("US");
// analytics.logEvent('MyApp startup',
// eventProperties: {'event_prop_1': 10, 'event_prop_2': true});
// analytics.logEvent('Out of Session Event', outOfSession: true);
// analytics.setOptOut(true);
// analytics.logEvent('Opt Out Event');
// analytics.setOptOut(false);

// Map<String, dynamic> userProps = {
// 'date': '01.06.2020',
// 'name': 'Name',
// 'buildNumber': '1.1.1',
// };
// analytics.logRevenueAmount(21.9);
// analytics.setUserProperties(userProps);
analytics = Amplitude(
Configuration(apiKey: widget.apiKey, logLevel: LogLevel.debug));
initAnalytics();
}

Future<void> _flushEvents() async {
// await analytics.uploadEvents();
analytics.flush();

setMessage('Events flushed.');
}
Expand Down Expand Up @@ -90,11 +74,11 @@ class _MyAppState extends State<MyApp> {
padding: const EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
DeviceIdSessionId(),
DeviceIdForm(),
divider,
UserIdForm(),
divider,
RegenerateDeviceBtn(),
ResetForm(),
divider,
EventForm(),
divider,
Expand All @@ -106,8 +90,8 @@ class _MyAppState extends State<MyApp> {
divider,
RevenueForm(),
divider,
FlushThresholdForm(),
divider,
// FlushThresholdForm(),
// divider,
ElevatedButton(
child: const Text('Flush Events'),
onPressed: _flushEvents,
Expand Down
Loading
Loading