Getting Started with Product Analytics
Use this quick start guide to set up a new instance of CSQ’s Product Analytics SDK in a Flutter application.
Once you’ve completed the setup process, the CSQ SDK will capture a wide variety of user interactions in your application with no additional code required.
Install the SDK
Section titled Install the SDKWith Flutter CLI:
flutter pub add contentsquareThis will add a line like this to your package’s pubspec.yaml (and run an implicit flutter pub get):
dependencies:flutter: sdk: fluttercontentsquare: ^4.0.0Start the SDK
Section titled Start the SDK-
Import the CSQ SDK in your app:
import 'package:contentsquare/csq.dart'; -
Configure the SDK as soon as possible in your app, ideally in the
main()function.main.dart import 'package:contentsquare/csq.dart';void main() async {await CSQ().configureProductAnalytics(environmentId: 'YOUR_ENVIRONMENT_ID');} -
(Optional) Add
ProductAnalyticsOptions. For example if your Product Analytics environment is hosted in the EU, set thebaseURLoption tohttps://mh.ba.contentsquare.netmain.dart import 'package:contentsquare/csq.dart';void main() async {await CSQ().configureProductAnalytics(environmentId: 'YOUR_ENVIRONMENT_ID',options: ProductAnalyticsOptions(baseUrl: 'https://mh.ba.contentsquare.net',),);} -
Add a call to
CSQ().start()after the configuration:main.dart import 'package:contentsquare/csq.dart';void main() async {await CSQ().configureProductAnalytics(environmentId: 'YOUR_ENVIRONMENT_ID',options: ProductAnalyticsOptions(baseUrl: 'https://mh.ba.contentsquare.net',),);await CSQ().start();} -
Start your application, and check logs for this output:
┌───────────────────────────────────────────────────────────────────────────────│ 🔔 IMPORTANT 🔔 (CSLIB 4.0.0)├───────────────────────────────────────────────────────────────────────────────│ Contentsquare Flutter SDK 4.0.0 starting in app:│ com.example.testapp└─────────────────────────────────────────────────────────────────────────────── -
Recommended To capture screen views automatically, add the
CSQNavigatorObserverto your app’s navigator observers:More details in Customize autocaptured screen names.main.dart import 'package:contentsquare/csq.dart';MaterialApp(navigatorObservers: [CSQNavigatorObserver(),],home: MyHomePage(),) -
Recommended In order to enable automatic capture of user interactions, add the option
enableInteractionsAutocapture: trueto yourProductAnalyticsOptionsconfiguration:main.dart import 'package:contentsquare/csq.dart';void main() async {await CSQ().configureProductAnalytics(environmentId: 'YOUR_ENVIRONMENT_ID',options: ProductAnalyticsOptions(enableInteractionsAutocapture: true,),);await CSQ().start();}
Get user consent
Section titled Get user consentImplement the optIn() API to forward user consent to the SDK and generate a user ID.
The CSQ SDK treats users as opted-out by default.
It can be done immediately after start is called:
void main() async { // ... await CSQ().start(); await CSQ().optIn(); // ...}Alternatively, you can call optIn() in response to a user action, such as tapping an “I agree” button:
class UserConsentScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('User Consent'), ), body: Center( child: ElevatedButton( onPressed: () async { await CSQ().optIn(); }, child: Text('Agree with Terms and Conditions'), ), ), ); }}Next steps
Section titled Next stepsOur SDK offers a wide range of features to enhance your implementation, including extended tracking capabilities, and personal data masking.
Session Replay and Error Monitoring are both available with the Experience Analytics Extension, which you can purchase separately.