Skip to content

Commit

Permalink
feat(posthog): support group analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Sep 6, 2024
1 parent f15231c commit e738c39
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 6 deletions.
45 changes: 40 additions & 5 deletions packages/posthog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const alias = async () => {

const capture = async () => {
await Posthog.capture({
event: 'test-event',
event: 'event',
properties: {
key: 'value',
},
Expand All @@ -49,6 +49,16 @@ const flush = async () => {
await Posthog.flush();
};

const group = async () => {
await Posthog.group({
type: 'group',
key: 'key',
groupProperties: {
key: 'value',
},
});
};

const identify = async () => {
await Posthog.identify({
distinctId: 'distinct-id',
Expand All @@ -71,7 +81,7 @@ const reset = async () => {

const screen = async () => {
await Posthog.screen({
screenTitle: 'test-screen',
screenTitle: 'screen',
properties: {
key: 'value',
},
Expand Down Expand Up @@ -99,6 +109,7 @@ const unregister = async () => {
* [`alias(...)`](#alias)
* [`capture(...)`](#capture)
* [`flush()`](#flush)
* [`group(...)`](#group)
* [`identify(...)`](#identify)
* [`register(...)`](#register)
* [`reset()`](#reset)
Expand Down Expand Up @@ -162,6 +173,23 @@ Only available on Android and iOS.
--------------------


### group(...)

```typescript
group(options: GroupOptions) => Promise<void>
```

Associate the events for that user with a group.

| Param | Type |
| ------------- | ----------------------------------------------------- |
| **`options`** | <code><a href="#groupoptions">GroupOptions</a></code> |

**Since:** 6.0.0

--------------------


### identify(...)

```typescript
Expand Down Expand Up @@ -282,6 +310,15 @@ Remove a super property.
| **`properties`** | <code><a href="#record">Record</a>&lt;string, any&gt;</code> | The properties to send with the event. | 6.0.0 |


#### GroupOptions

| Prop | Type | Description | Since |
| --------------------- | ------------------------------------------------------------ | -------------------------------------------- | ----- |
| **`type`** | <code>string</code> | The group type. | 6.0.0 |
| **`key`** | <code>string</code> | The group key. | 6.0.0 |
| **`groupProperties`** | <code><a href="#record">Record</a>&lt;string, any&gt;</code> | The properties to send with the group event. | 6.0.0 |


#### IdentifyOptions

| Prop | Type | Description | Since |
Expand Down Expand Up @@ -328,9 +365,7 @@ Remove a super property.

Construct a type with a set of properties K of type T

<code>{
[P in K]: T;
}</code>
<code>{ [P in K]: T; }</code>

</docgen-api>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.capawesome.capacitorjs.plugins.posthog.classes.options.SetupOptions

import com.posthog.android.PostHogAndroidConfig
import io.capawesome.capacitorjs.plugins.posthog.classes.options.AliasOptions
import io.capawesome.capacitorjs.plugins.posthog.classes.options.GroupOptions
import io.capawesome.capacitorjs.plugins.posthog.classes.options.UnregisterOptions

class Posthog(private val plugin: PosthogPlugin) {
Expand All @@ -28,6 +29,14 @@ class Posthog(private val plugin: PosthogPlugin) {
com.posthog.PostHog.flush()
}

fun group(options: GroupOptions) {
val type = options.type
val key = options.key
val groupProperties = options.groupProperties

com.posthog.PostHog.group(type = type, key = key, groupProperties = groupProperties)
}

fun identify(options: IdentifyOptions) {
val distinctId = options.distinctId
val userProperties = options.userProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.getcapacitor.annotation.CapacitorPlugin;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.AliasOptions;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.CaptureOptions;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.GroupOptions;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.IdentifyOptions;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.RegisterOptions;
import io.capawesome.capacitorjs.plugins.posthog.classes.options.ScreenOptions;
Expand All @@ -23,6 +24,7 @@ public class PosthogPlugin extends Plugin {
private static final String ERROR_DISTINCT_ID_MISSING = "distinctId must be provided.";
private static final String ERROR_KEY_MISSING = "key must be provided.";
private static final String ERROR_SCREEN_TITLE_MISSING = "screenTitle must be provided.";
private static final String ERROR_TYPE_MISSING = "type must be provided.";
private static final String ERROR_VALUE_MISSING = "value must be provided.";
private static final String ERROR_UNKNOWN_ERROR = "An unknown error has occurred.";
private static final String TAG = "PosthogPlugin";
Expand Down Expand Up @@ -85,6 +87,30 @@ public void flush(PluginCall call) {
}
}

@PluginMethod
public void group(PluginCall call) {
try {
String type = call.getString("type");
if (type == null) {
call.reject(ERROR_TYPE_MISSING);
return;
}
String key = call.getString("key");
if (key == null) {
call.reject(ERROR_KEY_MISSING);
return;
}
JSObject groupProperties = call.getObject("groupProperties");

GroupOptions options = new GroupOptions(type, key, groupProperties);

implementation.group(options);
call.resolve();
} catch (Exception exception) {
rejectCall(call, exception);
}
}

@PluginMethod
public void identify(PluginCall call) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.capawesome.capacitorjs.plugins.posthog.classes.options;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.getcapacitor.JSObject;

import org.json.JSONException;

import java.util.Map;

import io.capawesome.capacitorjs.plugins.posthog.PosthogHelper;

public class GroupOptions {
@NonNull
private String type;

@NonNull
private String key;

@Nullable
private Map<String, Object> groupProperties;

public GroupOptions(@NonNull String type, @NonNull String key, @Nullable JSObject groupProperties) throws JSONException {
this.type = type;
this.key = key;
this.groupProperties = PosthogHelper.createHashMapFromJSONObject(groupProperties);
}

@NonNull
public String getType() {
return type;
}

@NonNull
public String getKey() {
return key;
}

@Nullable
public Map<String, Object> getGroupProperties() {
return groupProperties;
}
}
4 changes: 4 additions & 0 deletions packages/posthog/ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
7C28C8CC2C8AE9CD00271553 /* SetupOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C28C8CB2C8AE9CD00271553 /* SetupOptions.swift */; };
7C28C8CE2C8AE9D800271553 /* UnregisterOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C28C8CD2C8AE9D800271553 /* UnregisterOptions.swift */; };
7C28C8D02C8AEA2E00271553 /* PosthogHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C28C8CF2C8AEA2E00271553 /* PosthogHelper.swift */; };
7CFD6A9E2C8B046100FF2B17 /* GroupOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CFD6A9D2C8B046100FF2B17 /* GroupOptions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -57,6 +58,7 @@
7C28C8CB2C8AE9CD00271553 /* SetupOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupOptions.swift; sourceTree = "<group>"; };
7C28C8CD2C8AE9D800271553 /* UnregisterOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnregisterOptions.swift; sourceTree = "<group>"; };
7C28C8CF2C8AEA2E00271553 /* PosthogHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PosthogHelper.swift; sourceTree = "<group>"; };
7CFD6A9D2C8B046100FF2B17 /* GroupOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupOptions.swift; sourceTree = "<group>"; };
91781294A431A2A7CC6EB714 /* Pods-Plugin.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.release.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.release.xcconfig"; sourceTree = "<group>"; };
96ED1B6440D6672E406C8D19 /* Pods-PluginTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PluginTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PluginTests/Pods-PluginTests.debug.xcconfig"; sourceTree = "<group>"; };
F65BB2953ECE002E1EF3E424 /* Pods-PluginTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PluginTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PluginTests/Pods-PluginTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -146,6 +148,7 @@
7C28C8C92C8AE9C300271553 /* ScreenOptions.swift */,
7C28C8CB2C8AE9CD00271553 /* SetupOptions.swift */,
7C28C8CD2C8AE9D800271553 /* UnregisterOptions.swift */,
7CFD6A9D2C8B046100FF2B17 /* GroupOptions.swift */,
);
path = Options;
sourceTree = "<group>";
Expand Down Expand Up @@ -358,6 +361,7 @@
7C28C8CE2C8AE9D800271553 /* UnregisterOptions.swift in Sources */,
50ADFFA82020EE4F00D50D53 /* PosthogPlugin.m in Sources */,
7C28C8C22C8AE99100271553 /* AliasOptions.swift in Sources */,
7CFD6A9E2C8B046100FF2B17 /* GroupOptions.swift in Sources */,
7C28C8C42C8AE99E00271553 /* CaptureOptions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
26 changes: 26 additions & 0 deletions packages/posthog/ios/Plugin/Classes/Options/GroupOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import Capacitor

@objc public class GroupOptions: NSObject {
private var type: String
private var key: String
private var groupProperties: [String: Any]?

init(type: String, key: String, groupProperties: [String: Any]?) {
self.type = type
self.key = key
self.groupProperties = groupProperties
}

func getType() -> String {
return type
}

func getKey() -> String {
return key
}

func getGroupProperties() -> [String: Any]? {
return groupProperties
}
}
8 changes: 8 additions & 0 deletions packages/posthog/ios/Plugin/Posthog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import PostHog
@objc public func flush() {
PostHogSDK.shared.flush()
}

@objc public func group(_ options: GroupOptions) {
let type = options.getType()
let key = options.getKey()
let groupProperties = options.getGroupProperties()

PostHogSDK.shared.group(type: type, key: key, groupProperties: groupProperties)
}

@objc public func identify(_ options: IdentifyOptions) {
let distinctId = options.getDistinctId()
Expand Down
1 change: 1 addition & 0 deletions packages/posthog/ios/Plugin/PosthogPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CAP_PLUGIN_METHOD(alias, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(capture, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(flush, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(group, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(identify, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(register, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(reset, CAPPluginReturnPromise);
Expand Down
25 changes: 24 additions & 1 deletion packages/posthog/ios/Plugin/PosthogPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PosthogPlugin: CAPPlugin {
public let errorEventMissing = "event must be provided."
public let errorKeyMissing = "key must be provided."
public let errorScreenTitleMissing = "screenTitle must be provided."
public let errorTypeMissing = "type must be provided."
public let errorValueMissing = "value must be provided."
private var implementation: Posthog?

Expand Down Expand Up @@ -50,6 +51,23 @@ public class PosthogPlugin: CAPPlugin {
implementation?.flush()
call.resolve()
}

@objc func group(_ call: CAPPluginCall) {
guard let type = call.getString("type") else {
call.reject(errorTypeMissing)
return
}
guard let key = call.getString("key") else {
call.reject(errorKeyMissing)
return
}
let groupProperties = call.getObject("groupProperties")

let options = GroupOptions(type: type, key: key, groupProperties: groupProperties)

implementation?.group(options)
call.resolve()
}

@objc func identify(_ call: CAPPluginCall) {
guard let distinctId = call.getString("distinctId") else {
Expand All @@ -69,10 +87,15 @@ public class PosthogPlugin: CAPPlugin {
call.reject(errorKeyMissing)
return
}
guard let value = call.getString("value") else {
guard let value = call.getObject("value") as AnyObject? else {
call.reject(errorValueMissing)
return
}

let options = RegisterOptions(key: key, value: value)

implementation?.register(options)
call.resolve()
}

@objc func reset(_ call: CAPPluginCall) {
Expand Down
32 changes: 32 additions & 0 deletions packages/posthog/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface PosthogPlugin {
* @since 6.0.0
*/
flush(): Promise<void>;
/**
* Associate the events for that user with a group.
*
* @since 6.0.0
*/
group(options: GroupOptions): Promise<void>;
/**
* Identify the current user.
*
Expand Down Expand Up @@ -91,6 +97,32 @@ export interface CaptureOptions {
properties?: Record<string, any>;
}

/**
* @since 6.0.0
*/
export interface GroupOptions {
/**
* The group type.
*
* @since 6.0.0
* @example 'company'
*/
type: string;
/**
* The group key.
*
* @since 6.0.0
* @example 'company_id_in_your_db'
*/
key: string;
/**
* The properties to send with the group event.
*
* @since 6.0.0
*/
groupProperties?: Record<string, any>;
}

/**
* @since 6.0.0
*/
Expand Down
Loading

0 comments on commit e738c39

Please sign in to comment.