|
| 1 | +# Countly Flutter Lite SDK |
| 2 | + |
| 3 | +This repository contains the non-bridged and lightweight Flutter Lite SDK which can be integrated into mobile application. The Countly Flutter Lite SDK is intended to be used with [Countly Lite](https://countly.com/lite), [Countly Flex](https://countly.com/flex), [Countly Enterprise](https://countly.com/enterprise). |
| 4 | + |
| 5 | +## What is Countly? |
| 6 | + |
| 7 | +[Countly](https://countly.com) is a product analytics solution and innovation enabler that helps teams track product performance and customer journey and behavior across [mobile](https://countly.com/mobile-analytics), [web](https://countly.com/web-analytics), and [desktop](https://countly.com/desktop-analytics) applications. |
| 8 | + |
| 9 | +Track, measure, and take action - all without leaving Countly. |
| 10 | + |
| 11 | +* **Questions or feature requests?** [Join the Countly Community on Discord](https://discord.gg/countly) |
| 12 | +* **Looking for the Countly Server?** [Countly Server repository](https://github.com/Countly/countly-server) |
| 13 | + |
| 14 | +## Integrating Countly SDK in your projects |
| 15 | + |
| 16 | +For a detailed description on how to use this SDK [check out our documentation](https://support.count.ly/hc/en-us/articles/). |
| 17 | + |
| 18 | +For an example integration of this SDK, see the [Flutter example app in this repository](https://github.com/Countly/countly-sdk-dart/tree/main/example/flutter_example). |
| 19 | + |
| 20 | +This SDK supports the following features: |
| 21 | + |
| 22 | +* Analytics |
| 23 | +* User Profiles |
| 24 | +* Views |
| 25 | +* Consent management |
| 26 | +* Device ID management |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +In the `dependencies:` section of your `pubspec.yaml`, add the following line: |
| 31 | + |
| 32 | +```yaml |
| 33 | +dependencies: |
| 34 | + countly_flutter_lite: <latest_version> |
| 35 | +``` |
| 36 | +
|
| 37 | +## Usage |
| 38 | +
|
| 39 | +### Initialization |
| 40 | +
|
| 41 | +```dart |
| 42 | +import 'package:countly_flutter_lite/countly.dart'; |
| 43 | +import 'package:flutter/widgets.dart'; |
| 44 | + |
| 45 | +void main() async { |
| 46 | + WidgetsFlutterBinding.ensureInitialized(); |
| 47 | + |
| 48 | + final config = CountlyConfig( |
| 49 | + appKey: 'YOUR_APP_KEY', |
| 50 | + serverUrl: 'https://your.server.com', |
| 51 | + giveConsent: true, |
| 52 | + enableSDKLogs: true, |
| 53 | + ); |
| 54 | + |
| 55 | + await Countly.init(config); |
| 56 | + runApp(const MyApp()); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Configuration Options |
| 61 | + |
| 62 | +```dart |
| 63 | +final config = CountlyConfig( |
| 64 | + appKey: 'YOUR_APP_KEY', // Required: Your Countly app key |
| 65 | + serverUrl: 'https://server.com', // Required: Your Countly server URL |
| 66 | + deviceId: 'custom-device-id', // Optional: Custom device ID |
| 67 | + giveConsent: true, // Optional: Grant consent at init |
| 68 | + startWithUnknownConsent: false, // Optional: Start with unknown consent |
| 69 | + enableSDKLogs: true, // Optional: Enable logging |
| 70 | + logLevel: LogLevel.verbose, // Optional: Log verbosity |
| 71 | + enableVisualWarnings: false, // Optional: Show visual warnings |
| 72 | + customRequestHeaders: { // Optional: Custom HTTP headers |
| 73 | + 'X-Custom-Header': 'value', |
| 74 | + }, |
| 75 | + deviceMetricOverrides: { // Optional: Override device metrics |
| 76 | + '_os': 'CustomOS', |
| 77 | + }, |
| 78 | + userProperties: { // Optional: Initial user properties |
| 79 | + 'name': 'John Doe', |
| 80 | + 'tier': 'premium', |
| 81 | + }, |
| 82 | + disableOldDataMigration: false, // Optional: Disable legacy migration |
| 83 | +); |
| 84 | +
|
| 85 | +// storageMode is optional: |
| 86 | +// - if omitted, Flutter Lite uses persistent storage |
| 87 | +// - if set (e.g. StorageMode.memory), your explicit value is respected |
| 88 | +
|
| 89 | +``` |
| 90 | + |
| 91 | +### Recording Events |
| 92 | + |
| 93 | +```dart |
| 94 | +final sdk = Countly.defaultInstance!; |
| 95 | +
|
| 96 | +await sdk.events.record(key: 'button_click'); |
| 97 | +await sdk.events.record(key: 'item_purchased', count: 3); |
| 98 | +await sdk.events.record(key: 'purchase', sum: 29.99); |
| 99 | +await sdk.events.record(key: 'video_watched', dur: 120.5); |
| 100 | +
|
| 101 | +await sdk.events.record( |
| 102 | + key: 'purchase', |
| 103 | + count: 1, |
| 104 | + sum: 99.99, |
| 105 | + dur: 5.0, |
| 106 | + segmentation: { |
| 107 | + 'product_id': 'SKU123', |
| 108 | + 'category': 'electronics', |
| 109 | + 'tags': ['featured', 'sale'], |
| 110 | + }, |
| 111 | +); |
| 112 | +``` |
| 113 | + |
| 114 | +### Recording Views |
| 115 | + |
| 116 | +```dart |
| 117 | +final sdk = Countly.defaultInstance!; |
| 118 | +
|
| 119 | +await sdk.views.startAutoStoppedView('HomePage'); |
| 120 | +await sdk.views.startAutoStoppedView('ProductPage', segmentation: { |
| 121 | + 'category': 'electronics', |
| 122 | +}); |
| 123 | +await sdk.views.endActiveView(segmentation: { |
| 124 | + 'exit': 'back_button', |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +### User Profiles |
| 129 | + |
| 130 | +```dart |
| 131 | +final sdk = Countly.defaultInstance!; |
| 132 | +
|
| 133 | +await sdk.users.setProperties({ |
| 134 | + 'name': 'John Doe', |
| 135 | + 'email': 'john@example.com', |
| 136 | + 'username': 'johndoe', |
| 137 | + 'phone': '+1234567890', |
| 138 | + 'gender': 'M', |
| 139 | + 'byear': 1990, |
| 140 | +}); |
| 141 | +
|
| 142 | +await sdk.users.pushToArray('viewed_products', ['SKU123', 'SKU456']); |
| 143 | +await sdk.users.addToSet('categories', ['electronics', 'books']); |
| 144 | +await sdk.users.pullFromArray('interests', ['outdated']); |
| 145 | +``` |
| 146 | + |
| 147 | +### Consent Management |
| 148 | + |
| 149 | +```dart |
| 150 | +final sdk = Countly.defaultInstance!; |
| 151 | +
|
| 152 | +await sdk.consents.giveConsent(); |
| 153 | +await sdk.consents.revokeConsent(); |
| 154 | +``` |
| 155 | + |
| 156 | +### Device ID Management |
| 157 | + |
| 158 | +```dart |
| 159 | +final sdk = Countly.defaultInstance!; |
| 160 | +
|
| 161 | +final deviceId = sdk.deviceId; |
| 162 | +await sdk.id.changeWithMerge('user_123456'); |
| 163 | +await sdk.id.changeWithoutMerge('new_anonymous_id'); |
| 164 | +await sdk.consents.giveConsent(); |
| 165 | +``` |
| 166 | + |
| 167 | +### Multi-Instance Support |
| 168 | + |
| 169 | +```dart |
| 170 | +final primary = await Countly.init(config); |
| 171 | +final secondary = await Countly.init(config2, instanceKey: 'secondary'); |
| 172 | +
|
| 173 | +final primaryInstance = Countly.defaultInstance; |
| 174 | +final secondaryInstance = Countly.instance('secondary'); |
| 175 | +
|
| 176 | +await Countly.disposeInstance('secondary'); |
| 177 | +await Countly.disposeAll(); |
| 178 | +``` |
| 179 | + |
| 180 | +### Custom Logger |
| 181 | + |
| 182 | +```dart |
| 183 | +class MyLogger implements SdkLogger { |
| 184 | + @override |
| 185 | + bool isEnabled(LogLevel level) => level.index <= LogLevel.info.index; |
| 186 | +
|
| 187 | + @override |
| 188 | + void log(LogLevel level, String message, {Object? error, StackTrace? stack}) { |
| 189 | + print('[${level.name}] $message'); |
| 190 | + } |
| 191 | +} |
| 192 | +
|
| 193 | +final config = CountlyConfig( |
| 194 | + appKey: 'YOUR_APP_KEY', |
| 195 | + serverUrl: 'https://your.server.com', |
| 196 | + logger: MyLogger(), |
| 197 | + enableSDKLogs: true, |
| 198 | +); |
| 199 | +``` |
| 200 | + |
| 201 | +## Security |
| 202 | + |
| 203 | +Security is very important to us. If you discover any issue regarding security, please disclose the information responsibly by sending an email to <security@countly.com> and **not by creating a GitHub issue**. |
| 204 | + |
| 205 | +## Badges |
| 206 | + |
| 207 | +If you like Countly, [why not use one of our badges](https://countly.com/brand-assets) and give a link back to us so others know about this wonderful platform? |
| 208 | + |
| 209 | +<a href="https://count.ly/f/badge" rel="nofollow"><img style="width:145px;height:60px" src="https://countly.com/badges/dark.svg?v2" alt="Countly - Product Analytics" /></a> |
| 210 | + |
| 211 | +```JS |
| 212 | +<a href="https://count.ly/f/badge" rel="nofollow"><img style="width:145px;height:60px" src="https://countly.com/badges/dark.svg" alt="Countly - Product Analytics" /></a> |
| 213 | +``` |
| 214 | + |
| 215 | +<a href="https://count.ly/f/badge" rel="nofollow"><img style="width:145px;height:60px" src="https://countly.com/badges/light.svg?v2" alt="Countly - Product Analytics" /></a> |
| 216 | + |
| 217 | +```JS |
| 218 | +<a href="https://count.ly/f/badge" rel="nofollow"><img style="width:145px;height:60px" src="https://countly.com/badges/light.svg" alt="Countly - Product Analytics" /></a> |
| 219 | +``` |
| 220 | + |
| 221 | +## How can I help you with your efforts? |
| 222 | + |
| 223 | +Glad you asked! For community support, feature requests, and engaging with the Countly Community, please join us at [our Discord Server](https://discord.gg/countly). We're excited to have you there! |
| 224 | + |
| 225 | +Also, we are on [Twitter](https://twitter.com/gocountly) and [LinkedIn](https://www.linkedin.com/company/countly) if you would like to keep up with Countly related updates. |
0 commit comments