Flutter市场调研插件flutter_pollfish的使用

发布于 1周前 作者 itying888 来自 Flutter

以下是根据您的需求整理的内容,详细介绍了如何在Flutter应用中使用flutter_pollfish插件:

Flutter市场调研插件flutter_pollfish的使用

前置条件

  • Android SDK 21或更高版本,并使用Google Play服务
  • iOS版本 11.0或更高版本
  • Flutter版本 1.20.0或更高版本
  • Dart SDK版本 2.12.0或更高版本
  • CocoaPods版本 1.10.0或更高版本

注意:

  • 如果您的应用面向儿童和家庭计划设计,则不应使用Pollfish SDK,因为Pollfish不会收集16岁以下用户的响应。
  • Pollfish iOS SDK利用Apple的广告标识符(IDFA)来识别并重新定位用户。从iOS 14开始,如果用户未授予IDFA权限,则不应初始化Pollfish Flutter插件。

快速指南

  1. 注册一个Pollfish开发者账户。
  2. 为每个目标平台(Android和iOS)创建新的应用并获取API密钥。
  3. 安装Pollfish插件并调用初始化函数。
  4. 设置发布模式并在应用商店发布。
  5. 更新您的应用隐私政策。
  6. 请求您的账户在Pollfish仪表板上进行验证。

迁移到v4

Pollfish Flutter插件v4引入了不同的API,并增加了初始化时的自定义选项。如果您已经在应用中集成了v4之前的Pollfish Flutter插件,请阅读以下迁移指南。

迁移指南

初始化

// v4之前的初始化方式
FlutterPollfish.instance.init(
    apiKey: 'YOUR_API_KEY',
    pollfishPosition: 5,
    indicatorPadding: 40,
    rewardMode: false,
    releaseMode: true,
    requestUUID: 'REQUEST_UUID',
    offerwallMode: false,
    userProperties: <String, dynamic>{ 
      'gender': '1',
      'education': '1',
      ...
    });

// v4及之后的初始化方式
FlutterPollfish.instance.init(
    androidApiKey: 'ANDROID_API_KEY', // 必需
    iOSApiKey: 'IOS_API_KEY',         // 必需
    pollfishPosition: Position.bottomRight,
    indicatorPadding: 40,
    rewardMode: false,
    releaseMode: true,
    requestUUID: 'REQUEST_UUID',
    offerwallMode: false,
    userProperties: <String, dynamic>{ 
      'gender': '1',
      'education': '1',
      ...
    });

分析步骤

1. 获取开发者账户

www.pollfish.com注册为开发者。

2. 在Pollfish面板中添加新应用并复制给定的API密钥

登录到www.pollfish.com,在Pollfish面板的"My Apps"部分为每个目标平台(Android和iOS)添加新应用,并复制给定的API密钥以供稍后在应用中的初始化函数中使用。

3. 安装插件

pubspec.yaml文件中添加以下依赖项:

dependencies:
  ...
  flutter_pollfish: ^4.2.0

然后执行以下命令:

flutter packages get

4. 导入flutter_pollfish.dart

import 'package:flutter_pollfish/flutter_pollfish.dart';

5. 初始化Pollfish

导入Pollfish插件:

import 'package:flutter_pollfish/flutter_pollfish.dart';

必须使用一个或两个API密钥初始化Pollfish插件,具体取决于您要针对哪个平台。您可以在Pollfish仪表板中创建新应用时获取API密钥。

FlutterPollfish.instance.init(androidApiKey: 'ANDROID_API_KEY', iOSApiKey: 'IOS_API_KEY', rewardMode: true); // Android和iOS
FlutterPollfish.instance.init(androidApiKey: 'ANDROID_API_KEY', iOSApiKey: null, rewardMode: true); // Android仅
FlutterPollfish.instance.init(androidApiKey: null, iOSApiKey: 'IOS_API_KEY', rewardMode: true); // iOS仅

配置Pollfish行为(可选)

您可以设置几个参数来控制Pollfish调查面板在应用中的行为。以下是可以使用的所有可用选项。

指示器位置

FlutterPollfish.instance.init(
  ...
  indicatorPosition: Position.topRight
);

指示器内边距

FlutterPollfish.instance.init(
  ...
  indicatorPadding: 8
);

Offerwall模式

FlutterPollfish.instance.init(
  ...
  offerwallMode: true
);

发布模式

FlutterPollfish.instance.init(
  ...
  releaseMode: true
);

奖励模式

FlutterPollfish.instance.init(
  ...
  rewardMode: true
);

请求UUID

FlutterPollfish.instance.init(
  ...
  requestUUID: "REQUEST_UUID"
);

用户属性

final userProperties = {
	"gender": "1",
	"year_of_birth": "1974",
	"marital_status": "2",
	"parental": "3",
	"education": "1",
	"employment": "1",
	"career": "2",
	"race": "3",
	"income": "1",
};

FlutterPollfish.instance.init(
  ...
  userProperties: userProperties
);

奖励信息

final rewardInfo = RewardInfo('Dollars', 1.2);

FlutterPollfish.instance.init(
  ...
  rewardInfo: rewardInfo
);

点击ID

FlutterPollfish.instance.init(
  ...
  clickId: "CLICK_ID"
);

用户ID

FlutterPollfish.instance.init(
  ...
  userId: "USER_ID"
);

签名

FlutterPollfish.instance.init(
  ...
  signature: "SIGNATURE"
);

放置ID

FlutterPollfish.instance.init(
  ...
  placementId: "PLACEMENT_ID"
);

示例

FlutterPollfish.instance.init(
  androidApiKey: 'ANDROID_API_KEY',
  iOSApiKey: 'IOS_API_KEY',
  rewardMode: true);

更新您的隐私政策

在您的应用隐私政策中添加以下段落:

“调查服务技术”
本应用使用Pollfish SDK。Pollfish是一个在线调查平台,任何人都可以通过它进行调查。Pollfish与智能手机应用的出版商合作,以便访问这些应用的用户并向他们发送调查问卷。当用户连接到此应用时,特定的设备数据(包括广告ID、设备ID、其他可用电子标识符以及进一步的响应元数据)将自动通过我们的应用发送到Pollfish服务器,以便Pollfish判断用户是否符合调查资格。有关Pollfish通过本应用接收的数据列表,请仔细阅读位于https://www.pollfish.com/terms/respondent的Pollfish受访者条款。这些数据将与Pollfish发送给符合条件的用户的问卷答案相关联。Pollfish可能会出于商业目的与第三方、客户和业务伙伴共享此类数据。下载应用程序即表示您同意本隐私政策文件,并同意Pollfish处理上述数据。此外,请注意,您随时可以通过访问https://www.pollfish.com/respondent/opt-out来禁用Pollfish操作。我们再次邀请您查看Pollfish受访者的使用条款,如果您希望更详细地了解Pollfish的工作方式以及Pollfish可能与谁进一步共享您的数据。

请求您的账户进行验证

在您的应用发布到应用商店后,应请求在您的Pollfish开发者仪表板上进行账户验证。

可选部分

实现Pollfish事件监听器

当收到Pollfish调查时的通知

FlutterPollfish.instance.setPollfishSurveyReceivedListener(onPollfishSurveyReceived);

void onPollfishSurveyReceived(SurveyInfo? surveyInfo) => setState(() {
  if (surveyInfo == null) {
    print("Offerwall Ready");
  } else {
    print("Survey Received - ${surveyInfo.toString()}");
  }
});

当完成Pollfish调查时的通知

FlutterPollfish.instance.setPollfishSurveyCompletedListener(onPollfishSurveyCompleted);

void onPollfishSurveyCompleted(SurveyInfo sureyInfo) => setState(() {
    print('Survey Completed: - ${surveyInfo.toString()}');
});

当用户不符合Pollfish调查资格时的通知

FlutterPollfish.instance.setPollfishUserNotEligibleListener(onPollfishUserNotEligible);

void onPollfishUserNotEligible() => setState(() {
   print('User Not Eligible');
}

当Pollfish调查不可用时的通知

FlutterPollfish.instance.setPollfishSurveyNotAvailableListener(onPollfishSurveyNotAvailable);

void onPollfishSurveyNotAvailable() => setState(() {
   print('Survey Not Available');
}

当用户拒绝Pollfish调查时的通知

FlutterPollfish.instance.setPollfishUserRejectedSurveyListener(onPollfishUserRejectedSurvey);

void onPollfishUserRejectedSurvey() => setState(() {
   print('User Rejected Survey');
}

当Pollfish调查面板打开时的通知

FlutterPollfish.instance.setPollfishOpenedListener(onPollfishOpened);

void onPollfishOpened() => setState(() {
   print('Survey Panel Open');
}

当Pollfish调查面板关闭时的通知

FlutterPollfish.instance.setPollfishClosedListener(onPollfishClosed);

void onPollfishClosed() => setState(() {
   print('Survey Panel Closed');
}

取消订阅Pollfish监听器

[@override](/user/override)
void dispose() {
    FlutterPollfish.instance.removeListeners();
    super.dispose();
}

手动显示/隐藏Pollfish面板

在调查期间,您可以调用以下函数手动隐藏和显示Pollfish。

FlutterPollfish.instance.show();

或者

FlutterPollfish.instance.hide();

检查设备上是否存在Pollfish调查

FlutterPollfish.instance.isPollfishPresent().then((isPresent) => {
  print("isPresent: $isPresent");
});

检查Pollfish面板是否打开

FlutterPollfish.instance.isPollfishPanelOpen().then((isOpen) => {
  print("isOpen: $isOpen");
});

示例代码

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_pollfish/flutter_pollfish.dart';

void main() => runApp(MyApp());

const String androidApiKey = 'ANDROID_API_KEY';
const String iOSApiKey = 'IOS_API_KEY';
const bool releaseMode = false;

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _logText = '';

  bool _showButton = false;
  bool _completedSurvey = false;
  int _currentIndex = 0;
  int _cpa = 0;

  [@override](/user/override)
  void initState() {
    super.initState();
    initPollfish();
  }

  [@override](/user/override)
  void dispose() {
    FlutterPollfish.instance.removeListeners();
    super.dispose();
  }

  Future<void> initPollfish() async {
    String logText = 'Initializing Pollfish...';

    _showButton = false;
    _completedSurvey = false;

    final offerwallMode = _currentIndex == 1;

    FlutterPollfish.instance.init(
        androidApiKey: androidApiKey,
        iosApiKey: iOSApiKey,
        rewardMode: true,
        releaseMode: releaseMode,
        offerwallMode: offerwallMode);

    FlutterPollfish.instance
        .setPollfishSurveyReceivedListener(onPollfishSurveyReceived);

    FlutterPollfish.instance
        .setPollfishSurveyCompletedListener(onPollfishSurveyCompleted);

    FlutterPollfish.instance.setPollfishOpenedListener(onPollfishOpened);

    FlutterPollfish.instance.setPollfishClosedListener(onPollfishClosed);

    FlutterPollfish.instance
        .setPollfishSurveyNotAvailableListener(onPollfishSurveyNotAvailable);

    FlutterPollfish.instance
        .setPollfishUserRejectedSurveyListener(onPollfishUserRejectedSurvey);

    FlutterPollfish.instance
        .setPollfishUserNotEligibleListener(onPollfishUserNotEligible);

    setState(() {
      _logText = logText;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: findCurrentTitle(_currentIndex),
          ),
          body: Center(
              child: new Container(
                  padding: new EdgeInsets.all(20.0),
                  child: new Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text('$_logText\n'),
                        (_showButton && !_completedSurvey)
                            ? new RawMaterialButton(
                                onPressed: () {
                                  FlutterPollfish.instance.show();
                                },
                                child: new Text(
                                    (_currentIndex == 0)
                                        ? 'Complete a Survey and Earn $_cpa Credits'
                                        : 'Offerwall - Take Surveys',
                                    style: new TextStyle(
                                        color: Colors.white, fontSize: 16.0)),
                                shape: new RoundedRectangleBorder(),
                                elevation: 2.0,
                                fillColor: Colors.blue,
                                padding: const EdgeInsets.all(15.0),
                              )
                            : (_currentIndex % 2 == 1 && !_showButton && _completedSurvey)
                                ? Container(
                                    child: new Text('You earned $_cpa Credits'),
                                  )
                                : Container()
                      ]))),
          bottomNavigationBar: BottomNavigationBar(
              onTap: onTabTapped,
              currentIndex: _currentIndex,
              items: [
                new BottomNavigationBarItem(
                  icon: Icon(Icons.card_giftcard),
                  label: 'Rewarded Survey',
                ),
                new BottomNavigationBarItem(
                    icon: Icon(Icons.local_offer), label: 'Offerwall')
              ])),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
    initPollfish();
  }

  Text findCurrentTitle(int currentIndex) {
    if (_currentIndex == 0) {
      return const Text('Pollfish Rewarded Integration');
    } else {
      return const Text('Pollfish Offerwall Integration');
    }
  }

  void onPollfishSurveyReceived(SurveyInfo? surveyInfo) => setState(() {
        if (surveyInfo == null) {
          _logText = 'Offerwall Ready';
        } else {
          _logText =
              'Survey Received: - ${surveyInfo.toString().replaceAll("\n", " ")}';
          _cpa = surveyInfo.surveyCPA ?? 0;
        }

        print(_logText);

        _completedSurvey = false;
        _showButton = true;
      });

  void onPollfishSurveyCompleted(SurveyInfo? surveyInfo) => setState(() {
        _logText = 'Survey Completed: - ${surveyInfo.toString()}';

        print(_logText);

        if (_currentIndex == 1) {
          _showButton = false;
          _completedSurvey = true;
        }
      });

  void onPollfishOpened() => setState(() {
        _logText = 'Survey Panel Open';

        print(_logText);
      });

  void onPollfishClosed() => setState(() {
        _logText = 'Survey Panel Closed';

        print(_logText);
      });

  void onPollfishSurveyNotAvailable() => setState(() {
        _logText = 'Survey Not Available';

        print(_logText);

        _showButton = false;
        _completedSurvey = false;
      });

  void onPollfishUserRejectedSurvey() => setState(() {
        _logText = 'User Rejected Survey';

        print(_logText);

        if (_currentIndex == 1) {
          _showButton = false;
          _completedSurvey = false;
        }
      });

  void onPollfishUserNotEligible() => setState(() {
        _logText = 'User Not Eligible';

        print(_logText);

        if (_currentIndex == 1) {
          _showButton = false;
          _completedSurvey = false;
        }
      });
}

更多关于Flutter市场调研插件flutter_pollfish的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter市场调研插件flutter_pollfish的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,关于在Flutter中使用flutter_pollfish插件进行市场调研,下面是一个简单的代码示例,展示了如何集成和使用该插件。

首先,确保你已经在pubspec.yaml文件中添加了flutter_pollfish依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_pollfish: ^最新版本号  # 请替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

接下来,你需要设置Pollfish的API密钥。这通常在你的Pollfish开发者账户中可以找到。确保不要在客户端代码中硬编码敏感信息,最好是通过环境变量或安全的配置管理工具来获取这些密钥。

以下是一个示例代码,展示了如何初始化Pollfish并在Flutter应用中执行一个简单的市场调研问卷:

import 'package:flutter/material.dart';
import 'package:flutter_pollfish/flutter_pollfish.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _result = "";

  @override
  void initState() {
    super.initState();
    // 假设你的API密钥存储在一个安全的地方,这里只是示例
    String apiKey = "YOUR_POLLFISH_API_KEY";
    
    // 初始化Pollfish客户端
    Pollfish.initialize(apiKey).then((success) {
      if (success) {
        // 初始化成功后,执行市场调研问卷
        _showSurvey();
      } else {
        setState(() {
          _result = "Failed to initialize Pollfish.";
        });
      }
    }).catchError((error) {
      setState(() {
        _result = "Error initializing Pollfish: $error";
      });
    });
  }

  Future<void> _showSurvey() async {
    try {
      // 展示市场调研问卷,问卷ID可以从Pollfish后台获取
      String surveyId = "YOUR_SURVEY_ID";
      PollfishResult result = await Pollfish.showSurvey(surveyId);
      
      if (result != null) {
        setState(() {
          _result = "Survey completed with response: ${result.responseData}";
        });
      } else {
        setState(() {
          _result = "Survey not completed.";
        });
      }
    } catch (error) {
      setState(() {
        _result = "Error showing survey: $error";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Pollfish Demo'),
        ),
        body: Center(
          child: Text(
            _result,
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们首先初始化flutter_pollfish插件,传入从Pollfish开发者账户获取的API密钥。
  2. 初始化成功后,我们调用Pollfish.showSurvey方法来显示指定的市场调研问卷。
  3. 问卷完成后,我们通过PollfishResult对象获取用户的响应数据,并在UI中显示。

请注意,你需要替换YOUR_POLLFISH_API_KEYYOUR_SURVEY_ID为实际的API密钥和问卷ID。同时,考虑到API密钥的安全性,建议不要在客户端代码中直接硬编码这些敏感信息。

此外,根据Pollfish的文档和API变化,上述代码可能需要做相应的调整。确保查阅最新的Pollfish Flutter插件文档以获取最新的使用方法和最佳实践。

回到顶部