Flutter广告与数据分析插件tamatem_plus的使用

Flutter广告与数据分析插件tamatem_plus的使用

功能特性

  • 实现了TamatemPlus的RESTful API:
    • 启动浏览器进行TamatemPlus授权
    • 获取Token
    • 连接TamatemPlus
    • 查询库存项目
    • 获取游戏玩家信息
    • 兑换库存项目
    • 登出

开始使用

首先在你的Flutter项目中添加tamatem_plus插件。在项目的pubspec.yaml文件中添加以下依赖:

flutter pub add tamatem_plus

使用步骤

  1. 在应用的根目录下添加.env文件,并填入以下内容:
TAMATEM_DOMAIN={TAMATEM_DOMAIN}
TAMATEM_CLIENT_ID={TAMATEM_CLIENT_ID}
TAMATEM_CUSTOM_SCHEME={TAMATEM_CUSTOM_SCHEME}
TAMATEM_GAME_STORE={TAMATEM_GAME_STORE}

其中,TAMATEM_CUSTOM_SCHEME是应用的深度链接方案,格式为{companyname}-{gamename}://{companyname}

  • TAMATEM_DOMAIN 是API域名,例如https://stg-be.tamatemplus.com/
  • TAMATEM_CLIENT_ID 是应用ID
  • TAMATEM_GAME_STORE 是由游戏制造商配置的游戏商店链接

然后将.env文件添加到应用的资源列表中:

flutter:
  assets:
    - .env
  1. 在应用启动时初始化插件:
import 'package:flutter/material.dart';
import 'package:tamatem_plus/tamatem_plus.dart';

void main() async {
  await TamatemPlusPlugin.init();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TamatemPlus Example'),
      ),
      body: Center(
        child: TamatemButton(
          child: Text(
            'Launch tamatem',
            style: TextStyle(color: Colors.red),
          ),
        ),
      ),
    );
  }
}
  1. TamatemButton嵌入到布局中,并根据需要自定义按钮样式。如上所示,在MyHomePage中直接使用TamatemButton即可。

许可证

   Copyright 2023 kuloud

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

更多关于Flutter广告与数据分析插件tamatem_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter广告与数据分析插件tamatem_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


tamatem_plus 是一个用于在 Flutter 应用中集成广告和数据分析功能的插件。它通常用于在中东地区的应用中展示广告,并提供用户行为分析功能。以下是如何在 Flutter 项目中使用 tamatem_plus 插件的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 tamatem_plus 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  tamatem_plus: ^1.0.0  # 请使用最新版本

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

2. 初始化插件

在使用 tamatem_plus 插件之前,你需要在应用的启动阶段初始化它。通常可以在 main.dart 文件中进行初始化。

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 TamatemPlus
  await TamatemPlus.initialize(
    appId: 'YOUR_APP_ID',  // 替换为你的 App ID
    appSecret: 'YOUR_APP_SECRET',  // 替换为你的 App Secret
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Tamatem Plus Example',
      home: HomeScreen(),
    );
  }
}

3. 展示广告

tamatem_plus 插件支持多种广告类型,例如横幅广告、插页式广告等。以下是如何展示横幅广告的示例:

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

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tamatem Plus Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Welcome to Tamatem Plus Example'),
            ElevatedButton(
              onPressed: () {
                // 展示横幅广告
                TamatemPlus.showBannerAd(
                  adUnitId: 'YOUR_BANNER_AD_UNIT_ID',  // 替换为你的横幅广告单元 ID
                  adSize: AdSize.banner,
                );
              },
              child: Text('Show Banner Ad'),
            ),
            ElevatedButton(
              onPressed: () async {
                // 展示插页式广告
                await TamatemPlus.showInterstitialAd(
                  adUnitId: 'YOUR_INTERSTITIAL_AD_UNIT_ID',  // 替换为你的插页式广告单元 ID
                );
              },
              child: Text('Show Interstitial Ad'),
            ),
          ],
        ),
      ),
    );
  }
}

4. 用户行为分析

tamatem_plus 插件还提供了用户行为分析的功能。你可以通过它来追踪用户的特定事件。

TamatemPlus.trackEvent(
  eventName: 'button_clicked',
  parameters: {
    'button_name': 'show_ad_button',
  },
);

5. 处理广告事件

你可以监听广告事件,例如广告加载成功、广告加载失败等。

TamatemPlus.setAdListener(
  onAdLoaded: (ad) {
    print('Ad loaded: $ad');
  },
  onAdFailedToLoad: (ad, error) {
    print('Ad failed to load: $ad, error: $error');
  },
  onAdOpened: (ad) {
    print('Ad opened: $ad');
  },
  onAdClosed: (ad) {
    print('Ad closed: $ad');
  },
);

6. 测试广告

在开发和测试阶段,你可以使用测试广告单元 ID 来确保广告功能正常工作。

TamatemPlus.showBannerAd(
  adUnitId: TamatemPlus.testBannerAdUnitId,
  adSize: AdSize.banner,
);

await TamatemPlus.showInterstitialAd(
  adUnitId: TamatemPlus.testInterstitialAdUnitId,
);

7. 发布应用

在发布应用之前,确保你已经将测试广告单元 ID 替换为实际的广告单元 ID。

8. 处理权限和合规性

根据应用的目标市场和广告平台的要求,确保你处理了所有必要的权限和合规性问题,例如 GDPR 或 CCPA。

9. 调试和日志

在开发过程中,你可以启用调试模式来查看更多的日志信息。

TamatemPlus.setDebugMode(true);
回到顶部