Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:qonversion_flutter/qonversion.dart';
import 'package:qonversion_flutter/qonversion_flutter.dart';

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

Expand Down
173 changes: 4 additions & 169 deletions lib/qonversion.dart
Original file line number Diff line number Diff line change
@@ -1,170 +1,5 @@
import 'dart:async';
import 'dart:io';
// This file exists to support old way to import package: 'package:qonversion_flutter/qonversion.dart'.
// Recommended way to import package no: 'package:qonversion_flutter/qonversion_flutter.dart'.

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'src/constants.dart';

enum QAttributionProvider { appsFlyer, branch }

class Qonversion {
static const MethodChannel _channel =
const MethodChannel('qonversion_flutter_sdk');

/// Launches Qonversion SDK with the given API keys for each platform:
/// [androidApiKey] and [iosApiKey] respectively,
/// you can get one in your account on qonversion.io.
///
/// Returns `userId` for Ads integrations.
///
/// **Warning**:
/// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically.
static Future<String> launch({
@required String androidApiKey,
@required String iosApiKey,
String userId,
}) async {
final apiKey = _obtainPlatformApiKey(
androidApiKey: androidApiKey,
iosApiKey: iosApiKey,
);

final args = {
Constants.kApiKey: apiKey,
Constants.kUserId: userId,
};

final uid = await _channel.invokeMethod(Constants.mLaunch, args);

return uid;
}

Future<String> trackPurchase(
Map<String, dynamic> details, Map<String, dynamic> purchase) async {
final args = {
Constants.kDetails: details,
Constants.kPurchase: purchase,
};

final uid = await _channel.invokeMethod(Constants.mTrackPurchase, args);

return uid;
}

/// Launches Qonversion SDK with the given API keys for each platform:
/// [androidApiKey] and [iosApiKey] respectively,
/// you can get one in your account on qonversion.io.
///
/// [onComplete] will return `uid` for Ads integrations.
///
/// **Warning**:
/// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically.
@Deprecated("Use `launch` method instead")
static Future<void> launchWith({
String androidApiKey,
String iosApiKey,
void Function(String) onComplete,
}) async {
final key = _obtainPlatformApiKey(
androidApiKey: androidApiKey,
iosApiKey: iosApiKey,
);

final args = {Constants.kApiKey: key};
final uid =
await _channel.invokeMethod(Constants.mLaunchWithKeyCompletion, args);

onComplete(uid);
}

/// Launches Qonversion SDK with the given API keys for each platform:
/// [androidApiKey] and [iosApiKey] respectively,
/// you can get one in your account on qonversion.io.
///
/// Sets client side [userid] (instead of Qonversion user-id) that will be used for matching data in the third party data.
@Deprecated("Use `launch` method instead")
static Future<void> launchWithClientSideUserId(
String userID, {
String androidApiKey,
String iosApiKey,
}) {
final key = _obtainPlatformApiKey(
androidApiKey: androidApiKey, iosApiKey: iosApiKey);

final args = {
Constants.kApiKey: key,
Constants.kUserId: userID,
};

return _channel.invokeMethod(Constants.mLaunchWithKeyUserId, args);
}

/// **Don't use with autoTrackPurchases: false** now.
/// Functionality is under development yet.
///
/// Launches Qonversion SDK with the given API keys for each platform:
/// [androidApiKey] and [iosApiKey] respectively,
/// you can get one in your account on qonversion.io.
///
/// With [autoTrackPurchases] parameter turned off you need to call `trackPurchase:transaction:` method.
/// [onComplete] will return `uid` for Ads integrations.
/// **Warning**:
/// Will track any purchase events (trials, subscriptions, basic purchases) automatically.
/// But if `autoTrackPurchases` disabled you need to call `trackPurchase:transaction:` method (under development yet).
/// Otherwise, purchases tracking won't work.
@Deprecated("Use `launch` method instead")
static Future<void> launchWithAutoTrackPurchases(
bool autoTrackPurchases, {
String androidApiKey,
String iosApiKey,
void Function(String) onComplete,
}) async {
final key = _obtainPlatformApiKey(
androidApiKey: androidApiKey, iosApiKey: iosApiKey);

final args = {
Constants.kApiKey: key,
Constants.kAutoTrackPurchases: autoTrackPurchases,
};
final uid = await _channel.invokeMethod<String>(
Constants.mLaunchWithKeyAutoTrackPurchasesCompletion, args);

onComplete(uid);
}

/// Sends your attribution [data] to the [provider].
///
/// [userID], if specified, will also be sent to the provider
static Future<void> addAttributionData(
Map<dynamic, dynamic> data, QAttributionProvider provider) {
final args = {
Constants.kData: data,
Constants.kProvider: describeEnum(provider),
};

return _channel.invokeMethod(Constants.mAddAttributionData, args);
}

static String _obtainPlatformApiKey({
String androidApiKey,
String iosApiKey,
}) {
String key;

if (Platform.isAndroid) {
key = androidApiKey;
} else if (Platform.isIOS) {
key = iosApiKey;
} else {
throw Exception('Unsupported platform');
}

if (key == null) {
throw Exception(
'Please provide API key for the platform you are running an app on');
}

return key;
}
}
export 'src/qonversion.dart';
export 'src/qa_provider.dart';
2 changes: 2 additions & 0 deletions lib/qonversion_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/qonversion.dart';
export 'src/qa_provider.dart';
1 change: 1 addition & 0 deletions lib/src/qa_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum QAttributionProvider { appsFlyer, branch }
88 changes: 88 additions & 0 deletions lib/src/qonversion.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'constants.dart';
import 'qa_provider.dart';

class Qonversion {
static const MethodChannel _channel =
const MethodChannel('qonversion_flutter_sdk');

/// Launches Qonversion SDK with the given API keys for each platform:
/// [androidApiKey] and [iosApiKey] respectively,
/// you can get one in your account on qonversion.io.
///
/// Returns `userId` for Ads integrations.
///
/// **Warning**:
/// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically.
static Future<String> launch({
@required String androidApiKey,
@required String iosApiKey,
String userId,
}) async {
final apiKey = _obtainPlatformApiKey(
androidApiKey: androidApiKey,
iosApiKey: iosApiKey,
);

final args = {
Constants.kApiKey: apiKey,
Constants.kUserId: userId,
};

final uid = await _channel.invokeMethod(Constants.mLaunch, args);

return uid;
}

Future<String> trackPurchase(
Map<String, dynamic> details, Map<String, dynamic> purchase) async {
final args = {
Constants.kDetails: details,
Constants.kPurchase: purchase,
};

final uid = await _channel.invokeMethod(Constants.mTrackPurchase, args);

return uid;
}

/// Sends your attribution [data] to the [provider].
///
/// [userID], if specified, will also be sent to the provider
static Future<void> addAttributionData(
Map<dynamic, dynamic> data, QAttributionProvider provider) {
final args = {
Constants.kData: data,
Constants.kProvider: describeEnum(provider),
};

return _channel.invokeMethod(Constants.mAddAttributionData, args);
}

static String _obtainPlatformApiKey({
String androidApiKey,
String iosApiKey,
}) {
String key;

if (Platform.isAndroid) {
key = androidApiKey;
} else if (Platform.isIOS) {
key = iosApiKey;
} else {
throw Exception('Unsupported platform');
}

if (key == null) {
throw Exception(
'Please provide API key for the platform you are running an app on');
}

return key;
}
}