Skip to content

Commit

Permalink
feat: 3843 - matomo for new product page (#4217)
Browse files Browse the repository at this point in the history
* feat: 3843 - matomo for new product page

Impacted files:
* `add_new_product_page.dart`: implemented the tracking of 6 events for matomo
* `analytics_helper.dart`: added 1 category and 6 related events for the "new product page"

* feat: 3843 - TraceableClientMixin
  • Loading branch information
monsieurtanuki authored Jun 20, 2023
1 parent 25eb15b commit 7ac86dc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 23 deletions.
25 changes: 25 additions & 0 deletions packages/smooth_app/lib/helpers/analytics_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum AnalyticsCategory {
share(tag: 'share'),
couldNotFindProduct(tag: 'could not find product'),
productEdit(tag: 'product edit'),
newProduct(tag: 'new product'),
list(tag: 'list'),
deepLink(tag: 'deep link');

Expand Down Expand Up @@ -47,6 +48,30 @@ enum AnalyticsEvent {
tag: 'opened product edit page',
category: AnalyticsCategory.productEdit,
),
openNewProductPage(
tag: 'opened new product page',
category: AnalyticsCategory.newProduct,
),
categoriesNewProductPage(
tag: 'set categories on new product page',
category: AnalyticsCategory.newProduct,
),
nutritionNewProductPage(
tag: 'set nutrition facts on new product page',
category: AnalyticsCategory.newProduct,
),
ingredientsNewProductPage(
tag: 'set ingredients on new product page',
category: AnalyticsCategory.newProduct,
),
imagesNewProductPage(
tag: 'set at least one image on new product page',
category: AnalyticsCategory.newProduct,
),
closeEmptyNewProductPage(
tag: 'closed new product page without any input',
category: AnalyticsCategory.newProduct,
),
shareList(tag: 'shared a list', category: AnalyticsCategory.list),
openListWeb(tag: 'open a list in wbe', category: AnalyticsCategory.list),
productDeepLink(
Expand Down
119 changes: 96 additions & 23 deletions packages/smooth_app/lib/pages/product/add_new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:matomo_tracker/matomo_tracker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/product_list.dart';
Expand All @@ -12,6 +13,7 @@ import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/svg_icon_chip.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/helpers/image_field_extension.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';
Expand Down Expand Up @@ -44,7 +46,8 @@ class AddNewProductPage extends StatefulWidget {
State<AddNewProductPage> createState() => _AddNewProductPageState();
}

class _AddNewProductPageState extends State<AddNewProductPage> {
class _AddNewProductPageState extends State<AddNewProductPage>
with TraceableClientMixin {
// Just one file per main image field
final Map<ImageField, File> _uploadedImages = <ImageField, File>{};

Expand Down Expand Up @@ -76,6 +79,17 @@ class _AddNewProductPageState extends State<AddNewProductPage> {

bool _ecoscoreExpanded = false;

bool _trackedPopulatedCategories = false;
bool _trackedPopulatedIngredients = false;
bool _trackedPopulatedNutrition = false;
bool _trackedPopulatedImages = false;

@override
String get traceName => 'Opened add_new_product_page';

@override
String get traceTitle => 'add_new_product_page';

@override
void initState() {
super.initState();
Expand All @@ -91,6 +105,10 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
_localDatabase = context.read<LocalDatabase>();
_localDatabase.upToDate.showInterest(barcode);
_daoProductList = DaoProductList(_localDatabase);
AnalyticsHelper.trackEvent(
AnalyticsEvent.openNewProductPage,
barcode: barcode,
);
}

@override
Expand Down Expand Up @@ -130,6 +148,12 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
),
),
);
if (leaveThePage == true) {
AnalyticsHelper.trackEvent(
AnalyticsEvent.closeEmptyNewProductPage,
barcode: barcode,
);
}
return leaveThePage ?? false;
},
child: SmoothScaffold(
Expand Down Expand Up @@ -251,7 +275,7 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
if (_ecoscoreExpanded) _buildEditorButton(context, _originEditor),
if (_ecoscoreExpanded) _buildEditorButton(context, _labelEditor),
if (_ecoscoreExpanded) _buildEditorButton(context, _packagingEditor),
if (_ecoscoreExpanded) _buildEditorButton(context, _ingredientsEditor),
if (_ecoscoreExpanded) _buildIngredientsButton(context),
];
}

Expand All @@ -268,9 +292,8 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
style: _getSubtitleStyle(context),
),
_buildCategoriesButton(context),
_buildEditorButton(
_buildIngredientsButton(
context,
_ingredientsEditor,
forceIconData: Icons.filter_2,
disabled: !_categoryEditor.isPopulated(_product),
),
Expand Down Expand Up @@ -340,6 +363,13 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
);
if (finalPhoto != null) {
setState(() {
if (!_trackedPopulatedImages) {
_trackedPopulatedImages = true;
AnalyticsHelper.trackEvent(
AnalyticsEvent.imagesNewProductPage,
barcode: barcode,
);
}
if (imageField == ImageField.OTHER) {
_otherUploadedImages.add(finalPhoto);
} else {
Expand All @@ -351,19 +381,30 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
done: imageFile != null,
);

Widget _buildNutritionInputButton(final BuildContext context) => _MyButton(
AppLocalizations.of(context).nutritional_facts_input_button_label,
Icons.filter_2,
// deactivated when the categories were not set beforehand
!_categoryEditor.isPopulated(_product)
? null
: () async => NutritionPageLoaded.showNutritionPage(
product: _product,
isLoggedInMandatory: false,
widget: this,
),
done: _nutritionFactsAdded,
);
Widget _buildNutritionInputButton(final BuildContext context) {
if (!_trackedPopulatedNutrition) {
if (_nutritionFactsAdded) {
_trackedPopulatedNutrition = true;
AnalyticsHelper.trackEvent(
AnalyticsEvent.nutritionNewProductPage,
barcode: barcode,
);
}
}
return _MyButton(
AppLocalizations.of(context).nutritional_facts_input_button_label,
Icons.filter_2,
// deactivated when the categories were not set beforehand
!_categoryEditor.isPopulated(_product)
? null
: () async => NutritionPageLoaded.showNutritionPage(
product: _product,
isLoggedInMandatory: false,
widget: this,
),
done: _nutritionFactsAdded,
);
}

Widget _buildEditorButton(
final BuildContext context,
Expand All @@ -386,12 +427,22 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
);
}

Widget _buildCategoriesButton(final BuildContext context) =>
_buildEditorButton(
context,
_categoryEditor,
forceIconData: Icons.filter_1,
);
Widget _buildCategoriesButton(final BuildContext context) {
if (!_trackedPopulatedCategories) {
if (_categoryEditor.isPopulated(_product)) {
_trackedPopulatedCategories = true;
AnalyticsHelper.trackEvent(
AnalyticsEvent.categoriesNewProductPage,
barcode: barcode,
);
}
}
return _buildEditorButton(
context,
_categoryEditor,
forceIconData: Icons.filter_1,
);
}

List<Widget> _getMiscRows(final BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
Expand All @@ -403,6 +454,28 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
_buildEditorButton(context, _detailsEditor),
];
}

Widget _buildIngredientsButton(
final BuildContext context, {
final IconData? forceIconData,
final bool disabled = false,
}) {
if (!_trackedPopulatedIngredients) {
if (_ingredientsEditor.isPopulated(_product)) {
_trackedPopulatedIngredients = true;
AnalyticsHelper.trackEvent(
AnalyticsEvent.ingredientsNewProductPage,
barcode: barcode,
);
}
}
return _buildEditorButton(
context,
_ingredientsEditor,
forceIconData: forceIconData,
disabled: disabled,
);
}
}

/// Standard button.
Expand Down

0 comments on commit 7ac86dc

Please sign in to comment.