Skip to main content

piano_consents_flutter 1.0.9 copy "piano_consents_flutter: ^1.0.9" to clipboard
piano_consents_flutter: ^1.0.9 copied to clipboard

Piano Consents SDK for Flutter.

Piano Consents SDK for Flutter #

Piano Consents Flutter SDK provides consent management for Piano products, allowing you to record and persist user consent choices across data processing purposes.

Installation #

Add the dependency to your pubspec.yaml:

dependencies:
  piano_consents_flutter: ^1.0.9

Getting Started #

Create a PianoConsentConfiguration and pass it to Piano.init:

import 'package:piano_common/piano_common.dart';
import 'package:piano_consents_flutter/piano_consents_flutter.dart';

final piano = await Piano.init(
  endpoint: PianoEndpoint.production,
  aid: '<AID>',
  consentConfig: PianoConsentConfiguration(requireConsent: true),
);

Access the consents manager via piano.client.consents.

Configuration #

PianoConsentConfiguration accepts two optional parameters:

Parameter Type Default Description
requireConsent bool false Whether explicit user consent is required
defaultPurposes Map<PianoProduct, PianoPurpose>? null Purposes of the listed products, overriding the built-in mappings below
final config = PianoConsentConfiguration(
  requireConsent: true,
  defaultPurposes: {
    PianoProduct.pa: PianoPurpose.audienceMeasurement,
    PianoProduct.dmp: PianoPurpose('CUSTOM'),
  },
);

Default product-to-purpose mappings #

Product Purpose
PianoProduct.pa PianoPurpose.audienceMeasurement
PianoProduct.dmp PianoPurpose.advertising
PianoProduct.composer PianoPurpose.contentPersonalisation
PianoProduct.id PianoPurpose.personalRelationship
PianoProduct.vx PianoPurpose.personalRelationship
PianoProduct.esp PianoPurpose.personalRelationship
PianoProduct.socialFlow PianoPurpose.advertising

Products left out of defaultPurposes keep these purposes. Once consents are stored, the purposes stored with them are restored on the next launch instead: defaultPurposes then applies only to the products they don't assign, and again after clear().

The consents of the products left out are reported like any other: once a consent is set, a purpose without a mode of its own is opt-in. If your consent dialog doesn't show their purposes, set a mode for them as well, such as with setAll or with set(…, PianoConsentMode.optOut).

piano.client.consents.set(PianoPurpose.audienceMeasurement, PianoConsentMode.optIn);

Optionally specify which products this consent applies to:

piano.client.consents.set(
  PianoPurpose.audienceMeasurement,
  PianoConsentMode.optIn,
  [PianoProduct.pa],
);

Set the same mode for all purposes #

piano.client.consents.setAll(PianoConsentMode.optOut);

Clear all consents #

Resets all consents to the initial (not-acquired) state:

piano.client.consents.clear();
// Current consent state keyed by purpose
final consentMap = piano.client.consents.consents;
final mode = consentMap[PianoPurpose.audienceMeasurement]?.mode;
final products = consentMap[PianoPurpose.audienceMeasurement]?.products;

// Current product-to-purpose mapping
final mapping = piano.client.consents.productsToPurposesMapping;
final purpose = mapping[PianoProduct.pa];

Until a consent is set, every purpose is not acquired; afterwards a purpose without a mode of its own is opt-in. On the next launch, a purpose the stored purposes assign no product stays not acquired until a mode is set for it: either only products missing from the stored purposes bring it in, such as products an older version of the app left out of defaultPurposes, so it was not shown to the user, or its mode was kept by a version before 1.0.9, which lost the purposes on a restart and dropped the modes set afterwards. Such a version stored no purposes when they were the built-in ones, so after upgrading from it every purpose of those consents is not acquired: set the consents of your consent dialog again.

Encoding consents as JSON #

toJson() encodes the consents keyed by purpose alias, the format of the consent parameter of the Piano ID pages and of the cookie_consents parameter of the Composer tracking and template requests:

import 'dart:convert';

// {"AM":{"mode":"opt-in","products":["PA"]},...}
final json = jsonEncode(piano.client.consents.consents.toJson());

PianoConsent.toJson() encodes a single consent the same way, without the purpose. The Pn-Consents header of the Piano ID API takes a list of {"purpose", "mode", "products"} objects instead, which piano_id sends itself.

Mode Description
PianoConsentMode.optIn All data processed according to purposes
PianoConsentMode.essential Only essential and mandatory data processed
PianoConsentMode.optOut Only mandatory data processed
PianoConsentMode.custom Fine-grained per-purpose selection
PianoConsentMode.notAcquired Consent not yet collected from the user

Purposes #

Four predefined purposes are available:

PianoPurpose.audienceMeasurement   // alias: 'AM'
PianoPurpose.contentPersonalisation // alias: 'CP'
PianoPurpose.advertising           // alias: 'AD'
PianoPurpose.personalRelationship  // alias: 'PR'

Custom purposes #

Create a custom purpose by providing a unique alias (max 32 characters, must not conflict with reserved aliases AM, CP, AD, PR, DL):

final custom = PianoPurpose('MY_PURPOSE');
piano.client.consents.set(custom, PianoConsentMode.optIn, [PianoProduct.pa]);

Products #

Constant Alias Description
PianoProduct.pa PA Piano Analytics
PianoProduct.dmp DMP DMP
PianoProduct.composer COMPOSER Piano Composer
PianoProduct.id ID Piano ID
PianoProduct.vx VX Piano VX
PianoProduct.esp ESP ESP
PianoProduct.socialFlow SOCIAL_FLOW Social Flow

Error Handling #

try {
  piano.client.consents.set(PianoPurpose.audienceMeasurement, PianoConsentMode.optIn);
} on StateError catch (e) {
  // requireConsent is false — consent management is disabled
  print(e.message);
} on ArgumentError catch (e) {
  // Invalid mode (notAcquired) or undefined purpose without products
  print(e.message);
}

Consent state is persisted automatically using SharedPreferences and restored on the next app launch.

0
likes
140
points
1.19k
downloads

Documentation

API reference

Publisher

verified publisherpiano.io

Weekly Downloads

Piano Consents SDK for Flutter.

Homepage

License

Apache-2.0 (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on piano_consents_flutter