Skip to content

Commit eaf9685

Browse files
authored
feat: add setGroup API support (#114)
1 parent b93dcde commit eaf9685

10 files changed

+75
-6
lines changed

Assets/Amplitude/Amplitude.cs

+51-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class Amplitude {
1111
private static readonly string UnityLibraryName = "amplitude-unity";
12-
private static readonly string UnityLibraryVersion = "2.5.1";
12+
private static readonly string UnityLibraryVersion = "2.6.0";
1313

1414
private static Dictionary<string, Amplitude> instances;
1515
private static readonly object instanceLock = new object();
@@ -40,6 +40,10 @@ public class Amplitude {
4040
[DllImport ("__Internal")]
4141
private static extern void _Amplitude_setUserProperties(string instanceName, string propertiesJson);
4242
[DllImport ("__Internal")]
43+
private static extern void _Amplitude_setGroup(string instanceName, string groupType, string groupName);
44+
[DllImport ("__Internal")]
45+
private static extern void _Amplitude_setGroupWithStringArray(string instanceName, string groupType, string[] groupName, int length);
46+
[DllImport ("__Internal")]
4347
private static extern void _Amplitude_setOptOut(string instanceName, bool enabled);
4448
[DllImport ("__Internal")]
4549
private static extern void _Amplitude_setMinTimeBetweenSessionsMillis(string instanceName, long minTimeBetweenSessionsMillis);
@@ -568,6 +572,52 @@ public void setUserProperties(IDictionary<string, object> properties) {
568572
#endif
569573
}
570574

575+
/// <summary>
576+
/// Adds a user to a group or groups. You need to specify a groupType and groupName(s).
577+
/// For example you can group people by their organization. In that case groupType is "orgId", and groupName would be the actual ID(s). groupName can be a string or an array of strings to indicate a user in multiple groups.
578+
/// You can also call setGroup multiple times with different groupTypes to track multiple types of groups (up to 5 per app).
579+
/// **Note:** this will also set groupType: groupName as a user property.
580+
/// </summary>
581+
/// <param name="groupType"You need to specify a group type (for example orgId).</param>
582+
/// <param name="groupName"The value for the group name, can be a string. (for example for groupType orgId, the groupName would be the actual id number, like 15).</param>
583+
public void setGroup(string groupType, string groupName) {
584+
Log (string.Format("C# setGroup"));
585+
#if (UNITY_IPHONE || UNITY_TVOS)
586+
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.tvOS) {
587+
_Amplitude_setGroup(instanceName, groupType, groupName);
588+
}
589+
#endif
590+
591+
#if UNITY_ANDROID
592+
if (Application.platform == RuntimePlatform.Android) {
593+
pluginClass.CallStatic("setGroup", instanceName, groupType, groupName);
594+
}
595+
#endif
596+
}
597+
598+
/// <summary>
599+
/// Adds a user to a group or groups. You need to specify a groupType and groupName(s).
600+
/// For example you can group people by their organization. In that case groupType is "orgId", and groupName would be the actual ID(s). groupName can be a string or an array of strings to indicate a user in multiple groups.
601+
/// You can also call setGroup multiple times with different groupTypes to track multiple types of groups (up to 5 per app).
602+
/// **Note:** this will also set groupType: groupName as a user property.
603+
/// </summary>
604+
/// <param name="groupType"You need to specify a group type (for example sports).</param>
605+
/// <param name="groupName"The value for the group name, can be an array of strings. (for example for groupType orgId, the groupName would be array of sport, like [soccer, tennis]).</param>
606+
public void setGroup(string groupType, string[] groupName) {
607+
Log (string.Format("C# setGroup"));
608+
#if (UNITY_IPHONE || UNITY_TVOS)
609+
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.tvOS) {
610+
_Amplitude_setGroupWithStringArray(instanceName, groupType, groupName, groupName.Length);
611+
}
612+
#endif
613+
614+
#if UNITY_ANDROID
615+
if (Application.platform == RuntimePlatform.Android) {
616+
pluginClass.CallStatic("setGroup", instanceName, groupType, groupName);
617+
}
618+
#endif
619+
}
620+
571621
/// <summary>
572622
/// Enables tracking opt out.
573623
/// If the user wants to opt out of all tracking, use this method to enable opt out for them.

Assets/Editor/AmplitudeDependencies.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<repository>https://repo.maven.apache.org/maven2</repository>
55
</repositories>
66

7-
<androidPackage spec="com.amplitude:android-sdk:2.37.0">
7+
<androidPackage spec="com.amplitude:android-sdk:2.38.3">
88
<repositories>
99
<repository>https://maven.google.com</repository>
1010
</repositories>
Binary file not shown.
Binary file not shown.

Assets/Plugins/Android/com.amplitude.android-sdk-2.37.0.aar.meta Assets/Plugins/Android/com.amplitude.android-sdk-2.38.3.aar.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/iOS/Amplitude/AmplitudeCWrapper.m

+13
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ void _Amplitude_setUserProperties(const char* instanceName, const char* properti
145145
[[Amplitude instanceWithName:ToNSString(instanceName)] setUserProperties:ToNSDictionary(properties)];
146146
}
147147

148+
void _Amplitude_setGroup(const char* instanceName, const char* groupType, const char* groupName) {
149+
[[Amplitude instanceWithName:ToNSString(instanceName)] setGroup:ToNSString(groupType) groupName:ToNSString(groupName)];
150+
}
151+
152+
void _Amplitude_setGroupWithStringArray(const char* instanceName, const char* groupType, const char* groupName[], const int length) {
153+
if (length == 0) return;
154+
NSMutableArray *array = [NSMutableArray arrayWithCapacity:length];
155+
for (int i = 0; i < length; i++) {
156+
[array addObject:ToNSString(groupName[i])];
157+
}
158+
[[Amplitude instanceWithName:ToNSString(instanceName)] setGroup:ToNSString(groupType) groupName:array];
159+
}
160+
148161
void _Amplitude_setOptOut(const char* instanceName, const bool enabled) {
149162
[[Amplitude instanceWithName:ToNSString(instanceName)] setOptOut:enabled];
150163
}

Assets/Scripts/AmplitudeDemo.cs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void Start() {
5151
amplitude.removeUserProperty("longArray", new long[]{ 1111,2222,3333,4444,5555,6666});
5252
amplitude.setUserProperty("floatArray", new float[]{123.45f, 678.9f});
5353
amplitude.setUserProperty("doubleArray", new double[]{123.45, 678.9});
54+
amplitude.setGroup("sdk", "unity");
55+
amplitude.setGroup("sports", new string[]{"soccer", "tennis"});
5456

5557
Dictionary<string, object> dictValue = new Dictionary<string, object>()
5658
{

Assets/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.amplitude.unityplugin",
3-
"version": "2.5.1",
3+
"version": "2.6.0",
44
"displayName": "Amplitude Unity SDK",
55
"description": "A plugin to simplify the integration of Amplitude iOS and Android SDKs into your Unity project. This repository also contains a sample project with the Unity plugin integrated.",
66
"unity": "2021.3",

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Versioned Releases
22

3+
## 2.6.0
4+
* Upgrade nativeAndroid SDK to v2.38.3.
5+
* Add `setGroup` API support.
6+
37
## 2.5.1
48
* Upgrade nativeAndroid SDK to v2.37.0.
59
* Upgrade native iOS SDK to v8.10.0.

ProjectSettings/AndroidResolverDependencies.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<dependencies>
22
<packages>
3-
<package>com.amplitude:android-sdk:2.37.0</package>
3+
<package>com.amplitude:android-sdk:2.38.3</package>
44
<package>com.squareup.okhttp3:okhttp:4.2.2</package>
55
</packages>
66
<files>
77
<file>Assets/Plugins/Android/com.amplitude.analytics-connector-1.0.0.aar</file>
8-
<file>Assets/Plugins/Android/com.amplitude.android-sdk-2.37.0.aar</file>
8+
<file>Assets/Plugins/Android/com.amplitude.android-sdk-2.38.3.aar</file>
99
<file>Assets/Plugins/Android/com.squareup.okhttp3.okhttp-4.2.2.jar</file>
1010
<file>Assets/Plugins/Android/com.squareup.okio.okio-2.2.2.jar</file>
1111
<file>Assets/Plugins/Android/org.jetbrains.annotations-13.0.jar</file>

0 commit comments

Comments
 (0)