Skip to content

Commit

Permalink
further cleanups with capture method
Browse files Browse the repository at this point in the history
  • Loading branch information
fuziontech committed Jun 25, 2020
1 parent fb650e9 commit 2b1d716
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private void setupChannels(Context applicationContext, BinaryMessenger messenger

String writeKey = bundle.getString("com.posthog.posthog.API_KEY");
String posthogHost = bundle.getString("com.posthog.posthog.POSTHOG_HOST");
Boolean trackApplicationLifecycleEvents = bundle.getBoolean("com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS");
Boolean captureApplicationLifecycleEvents = bundle.getBoolean("com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS");
Boolean debug = bundle.getBoolean("com.posthog.posthog.DEBUG", false);

PostHog.Builder analyticsBuilder = new PostHog.Builder(applicationContext, writeKey, posthogHost);
if (trackApplicationLifecycleEvents) {
if (captureApplicationLifecycleEvents) {
// Enable this to record certain application events automatically
analyticsBuilder.captureApplicationLifecycleEvents();
}
Expand Down Expand Up @@ -121,8 +121,8 @@ public void onDetachedFromEngine(FlutterPluginBinding binding) { }
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("identify")) {
this.identify(call, result);
} else if (call.method.equals("track")) {
this.track(call, result);
} else if (call.method.equals("capture")) {
this.capture(call, result);
} else if (call.method.equals("screen")) {
this.screen(call, result);
} else if (call.method.equals("alias")) {
Expand Down Expand Up @@ -171,19 +171,19 @@ private void callIdentify(
PostHog.with(this.applicationContext).identify(userId, properties, options);
}

private void track(MethodCall call, Result result) {
private void capture(MethodCall call, Result result) {
try {
String eventName = call.argument("eventName");
HashMap<String, Object> propertiesData = call.argument("properties");
HashMap<String, Object> options = call.argument("options");
this.callTrack(eventName, propertiesData, options);
this.callCapture(eventName, propertiesData, options);
result.success(true);
} catch (Exception e) {
result.error("PosthogFlutterException", e.getLocalizedMessage(), null);
}
}

private void callTrack(
private void callCapture(
String eventName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
Expand Down
8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class MyApp extends StatelessWidget {
Spacer(),
Center(
child: FlatButton(
child: Text('TRACK ACTION WITH POSTHOG'),
child: Text('CAPTURE ACTION WITH POSTHOG'),
onPressed: () {
Posthog.track(
Posthog.capture(
eventName: 'ButtonClicked',
properties: {
'foo': 'bar',
Expand Down Expand Up @@ -83,7 +83,7 @@ class MyApp extends StatelessWidget {
child: Text('Disable'),
onPressed: () async {
await Posthog.disable();
Posthog.track(eventName: 'This event will not be logged');
Posthog.capture(eventName: 'This event will not be logged');
},
),
),
Expand All @@ -93,7 +93,7 @@ class MyApp extends StatelessWidget {
child: Text('Enable'),
onPressed: () async {
await Posthog.enable();
Posthog.track(eventName: 'Enabled tracking events!');
Posthog.capture(eventName: 'Enabled capturing events!');
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script>
!function () {
var analytics = window.analytics = window.analytics || []; if (!analytics.initialize) if (analytics.invoked) window.console && console.error && console.error("Posthog snippet included twice."); else {
analytics.invoked = !0; analytics.methods = ["trackSubmit", "trackClick", "trackLink", "trackForm", "pageview", "identify", "reset", "group", "track", "ready", "alias", "debug", "page", "once", "off", "on"]; analytics.factory = function (t) { return function () { var e = Array.prototype.slice.call(arguments); e.unshift(t); analytics.push(e); return analytics } }; for (var t = 0; t < analytics.methods.length; t++) { var e = analytics.methods[t]; analytics[e] = analytics.factory(e) } analytics.load = function (t, e) { var n = document.createElement("script"); n.type = "text/javascript"; n.async = !0; n.src = "https://cdn.posthog.com/analytics.js/v1/" + t + "/analytics.min.js"; var a = document.getElementsByTagName("script")[0]; a.parentNode.insertBefore(n, a); analytics._loadOptions = e }; analytics.SNIPPET_VERSION = "4.1.0";
analytics.invoked = !0; analytics.methods = ["captureSubmit", "captureClick", "captureLink", "captureForm", "pageview", "identify", "reset", "group", "capture", "ready", "alias", "debug", "page", "once", "off", "on"]; analytics.factory = function (t) { return function () { var e = Array.prototype.slice.call(arguments); e.unshift(t); analytics.push(e); return analytics } }; for (var t = 0; t < analytics.methods.length; t++) { var e = analytics.methods[t]; analytics[e] = analytics.factory(e) } analytics.load = function (t, e) { var n = document.createElement("script"); n.type = "text/javascript"; n.async = !0; n.src = "https://cdn.posthog.com/analytics.js/v1/" + t + "/analytics.min.js"; var a = document.getElementsByTagName("script")[0]; a.parentNode.insertBefore(n, a); analytics._loadOptions = e }; analytics.SNIPPET_VERSION = "4.1.0";
analytics.load("YOUR_API_KEY_GOES_HERE");
analytics.page();
}
Expand Down
6 changes: 3 additions & 3 deletions ios/Classes/PosthogFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"identify" isEqualToString:call.method]) {
[self identify:call result:result];
} else if ([@"track" isEqualToString:call.method]) {
[self track:call result:result];
} else if ([@"capture" isEqualToString:call.method]) {
[self capture:call result:result];
} else if ([@"screen" isEqualToString:call.method]) {
[self screen:call result:result];
} else if ([@"alias" isEqualToString:call.method]) {
Expand Down Expand Up @@ -152,7 +152,7 @@ - (void)identify:(FlutterMethodCall*)call result:(FlutterResult)result {
}
}

- (void)track:(FlutterMethodCall*)call result:(FlutterResult)result {
- (void)capture:(FlutterMethodCall*)call result:(FlutterResult)result {
@try {
NSString *eventName = call.arguments[@"eventName"];
NSDictionary *properties = call.arguments[@"properties"];
Expand Down
20 changes: 4 additions & 16 deletions lib/src/posthog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ class Posthog {

static Future<void> identify({
@required userId,
Map<String, dynamic> traits,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) {
return _posthog.identify(
userId: userId,
traits: traits,
properties: properties,
options: options,
);
}

static Future<void> track({
static Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) {
return _posthog.track(
return _posthog.capture(
eventName: eventName,
properties: properties,
options: options,
Expand All @@ -45,18 +45,6 @@ class Posthog {
);
}

static Future<void> group({
@required String groupId,
Map<String, dynamic> traits,
Map<String, dynamic> options,
}) {
return _posthog.group(
groupId: groupId,
traits: traits,
options: options,
);
}

static Future<void> alias({
@required String alias,
Map<String, dynamic> options,
Expand Down
24 changes: 4 additions & 20 deletions lib/src/posthog_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ const MethodChannel _channel = MethodChannel('posthog_flutter');
class PosthogMethodChannel extends PosthogPlatform {
Future<void> identify({
@required userId,
Map<String, dynamic> traits,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) async {
try {
await _channel.invokeMethod('identify', {
'userId': userId,
'traits': traits ?? {},
'properties': properties ?? {},
'options': options ?? PosthogDefaultOptions.instance.options ?? {},
});
} on PlatformException catch (exception) {
print(exception);
}
}

Future<void> track({
Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) async {
try {
await _channel.invokeMethod('track', {
await _channel.invokeMethod('capture', {
'eventName': eventName,
'properties': properties ?? {},
'options': options ?? PosthogDefaultOptions.instance.options ?? {},
Expand All @@ -54,22 +54,6 @@ class PosthogMethodChannel extends PosthogPlatform {
}
}

Future<void> group({
@required String groupId,
Map<String, dynamic> traits,
Map<String, dynamic> options,
}) async {
try {
await _channel.invokeMethod('group', {
'groupId': groupId,
'traits': traits ?? {},
'options': options ?? PosthogDefaultOptions.instance.options ?? {},
});
} on PlatformException catch (exception) {
print(exception);
}
}

Future<void> alias({
@required String alias,
Map<String, dynamic> options,
Expand Down
14 changes: 3 additions & 11 deletions lib/src/posthog_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ abstract class PosthogPlatform {

Future<void> identify({
@required userId,
Map<String, dynamic> traits,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) {
throw UnimplementedError('identify() has not been implemented.');
}

Future<void> track({
Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
}) {
throw UnimplementedError('track() has not been implemented.');
throw UnimplementedError('capture() has not been implemented.');
}

Future<void> screen({
Expand All @@ -35,14 +35,6 @@ abstract class PosthogPlatform {
throw UnimplementedError('screen() has not been implemented.');
}

Future<void> group({
@required String groupId,
Map<String, dynamic> traits,
Map<String, dynamic> options,
}) {
throw UnimplementedError('group() has not been implemented.');
}

Future<void> alias({
@required String alias,
Map<String, dynamic> options,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/posthog_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PosthogWeb {
]);
break;
case 'capture':
analytics.callMethod('track', [
analytics.callMethod('capture', [
call.arguments['eventName'],
JsObject.jsify(call.arguments['properties']),
]);
Expand Down

0 comments on commit 2b1d716

Please sign in to comment.