Skip to content

Commit

Permalink
feat: EU dynamic configuration support (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Oct 28, 2021
1 parent c6baef7 commit 4ea525a
Show file tree
Hide file tree
Showing 64 changed files with 653 additions and 1,506 deletions.
48 changes: 47 additions & 1 deletion Assets/Amplitude/Amplitude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class Amplitude {
private static readonly string UnityLibraryName = "amplitude-unity";
private static readonly string UnityLibraryVersion = "2.3.0";
private static readonly string UnityLibraryVersion = "2.4.0";

private static Dictionary<string, Amplitude> instances;
private static readonly object instanceLock = new object();
Expand Down Expand Up @@ -56,6 +56,10 @@ public class Amplitude {
[DllImport ("__Internal")]
private static extern void _Amplitude_setServerUrl(string instanceName, string serverUrl);
[DllImport ("__Internal")]
private static extern void _Amplitude_setServerZone(string instanceName, string serverZone, bool updateServerUrl);
[DllImport ("__Internal")]
private static extern void _Amplitude_setUseDynamicConfig(string instanceName, bool useDynamicConfig);
[DllImport ("__Internal")]
private static extern void _Amplitude_logRevenueAmount(string instanceName, double amount);
[DllImport ("__Internal")]
private static extern void _Amplitude_logRevenue(string instanceName, string productIdentifier, int quantity, double price);
Expand Down Expand Up @@ -703,6 +707,48 @@ public void setServerUrl(string serverUrl) {
#endif
}

/// <summary>
/// Turning dynamic config on will find the best server url automatically based on users' geo location.
/// Note:
/// 1. If you have your own proxy server and use `setServerUrl` API, please leave this off.
/// 2. If you have users in China Mainland, we suggest you turn this on.
/// </summary>
public void setUseDynamicConfig(bool useDynamicConfig) {
Log (string.Format("C# setUseDynamicConfig"));
#if (UNITY_IPHONE || UNITY_TVOS)
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.tvOS) {
_Amplitude_setUseDynamicConfig(instanceName, useDynamicConfig);
}
#endif

#if UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android) {
pluginClass.CallStatic("setUseDynamicConfig", instanceName, useDynamicConfig);
}
#endif
}

/// <summary>
/// Set Amplitude Server Zone, switch to zone related configuration, including dynamic configuration and server url.
/// To send data to Amplitude's EU servers, you need to configure the serverZone to EU like client.setServerZone("EU");
/// serverZone could be EU or US. Recommend to keep updateServerUrl to be true for alignment unless use own proxy server.
/// </summary>
public void setServerZone(AmplitudeServerZone serverZone, bool updateServerUrl = true) {
Log (string.Format("C# setServerZone"));
string serverZoneStr = serverZone.ToString();
#if (UNITY_IPHONE || UNITY_TVOS)
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.tvOS) {
_Amplitude_setServerZone(instanceName, serverZoneStr, updateServerUrl);
}
#endif

#if UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android) {
pluginClass.CallStatic("setServerZone", instanceName, serverZoneStr, updateServerUrl);
}
#endif
}

[System.Obsolete("Please call setUserProperties instead", false)]
public void setGlobalUserProperties(IDictionary<string, object> properties) {
setUserProperties(properties);
Expand Down
4 changes: 4 additions & 0 deletions Assets/Amplitude/AmplitudeServerZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum AmplitudeServerZone {
US,
EU
}
11 changes: 11 additions & 0 deletions Assets/Amplitude/AmplitudeServerZone.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Editor/AmplitudeDependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<repository>https://repo.maven.apache.org/maven2</repository>
</repositories>

<androidPackage spec="com.amplitude:android-sdk:2.32.0">
<androidPackage spec="com.amplitude:android-sdk:2.34.1">
<repositories>
<repository>https://maven.google.com</repository>
</repositories>
Expand Down
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/Plugins/iOS/Amplitude/AMPBackgroundNotifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AMPBackgroundNotifier.h
// Copyright (c) 2020 Amplitude Inc. (https://amplitude.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import <Foundation/Foundation.h>

#if TARGET_OS_WATCH

NS_ASSUME_NONNULL_BEGIN

extern NSNotificationName const AMPAppWillEnterForegroundNotification;
extern NSNotificationName const AMPAppDidEnterBackgroundNotification;

/// watchOS adds support for background notifications in watchOS 7.0 with `WKExtension.applicationDidEnterBackgroundNotification`
/// and related notifications. But since this SDK is backwards compatible with watchOS 3.0, these notifications are not available. Instead, the user
/// should implement the appropriate background methods on their Extension Delegate and call the `AMPBackgroundNotifier` from those
/// methods.
@interface AMPBackgroundNotifier : NSObject

+ (void)applicationWillEnterForeground;
+ (void)applicationDidEnterBackground;

@end

NS_ASSUME_NONNULL_END

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//
// AMPBubbleView.m
// Amplitude
//
// AMPBackgroundNotifier.m
// Copyright (c) 2020 Amplitude Inc. (https://amplitude.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -23,38 +21,23 @@
// THE SOFTWARE.
//

#import "AMPBubbleView.h"
#import "AMPBackgroundNotifier.h"

@implementation AMPBubbleView
#if TARGET_OS_WATCH

- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];

if (self) {
[self loadViewFromNib];
}

return self;
}
NSNotificationName const AMPAppWillEnterForegroundNotification = @"com.amplitude.appWillEnterForegroundNotification";
NSNotificationName const AMPAppDidEnterBackgroundNotification = @"com.amplitude.appDidEnterBackgroundNotification";

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
[self loadViewFromNib];
}

return self;
@implementation AMPBackgroundNotifier

+ (void)applicationWillEnterForeground {
[[NSNotificationCenter defaultCenter] postNotificationName:AMPAppWillEnterForegroundNotification object:self];
}

- (void)loadViewFromNib {
NSBundle *bundle = [NSBundle bundleForClass:[AMPBubbleView class]];
UINib *nib = [UINib nibWithNibName:@"AMPBubbleView" bundle:bundle];

NSArray *views = [nib instantiateWithOwner:self options:nil];
UIView *view = [views objectAtIndex:0];
view.frame = self.bounds;
[self addSubview:view];
+ (void)applicationDidEnterBackground {
[[NSNotificationCenter defaultCenter] postNotificationName:AMPAppDidEnterBackgroundNotification object:self];
}

@end

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 0 additions & 38 deletions Assets/Plugins/iOS/Amplitude/AMPBubbleView.xib

This file was deleted.

3 changes: 2 additions & 1 deletion Assets/Plugins/iOS/Amplitude/AMPConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//

#import <Foundation/Foundation.h>
#import "AMPServerZone.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -30,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) NSString *ingestionEndpoint;

+ (instancetype)sharedInstance;
- (void)refresh:(void(^)(void))completionHandler;
- (void)refresh:(void(^)(void))completionHandler serverZone:(AMPServerZone)serverZone;

@end

Expand Down
6 changes: 4 additions & 2 deletions Assets/Plugins/iOS/Amplitude/AMPConfigManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#import "AMPConfigManager.h"
#import "AMPConstants.h"
#import "AMPServerZone.h"
#import "AMPServerZoneUtil.h"

@interface AMPConfigManager ()

Expand All @@ -48,8 +50,8 @@ - (instancetype)init {
return self;
}

- (void)refresh:(void(^)(void))completionHandler {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kAMPDyanmicConfigUrl]];
- (void)refresh:(void(^)(void))completionHandler serverZone:(AMPServerZone)serverZone {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[AMPServerZoneUtil getDynamicConfigApi:serverZone]]];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
Expand Down
8 changes: 8 additions & 0 deletions Assets/Plugins/iOS/Amplitude/AMPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ extern NSString *const kAMPPlatform;
extern NSString *const kAMPOSName;
extern NSString *const kAMPEventLogDomain;
extern NSString *const kAMPEventLogUrl;
extern NSString *const kAMPEventLogEuUrl;
extern NSString *const kAMPContentTypeHeader;
extern NSString *const kAMPDyanmicConfigUrl;
extern NSString *const kAMPDyanmicConfigEuUrl;
extern NSString *const kAMPDefaultInstance;
extern const int kAMPApiVersion;
extern const int kAMPDBVersion;
Expand Down Expand Up @@ -82,3 +85,8 @@ extern NSString *const AMP_TRACKING_OPTION_OS_VERSION;
extern NSString *const AMP_TRACKING_OPTION_PLATFORM;
extern NSString *const AMP_TRACKING_OPTION_REGION;
extern NSString *const AMP_TRACKING_OPTION_VERSION_NAME;

// Plan
extern NSString *const AMP_PLAN_BRANCH;
extern NSString *const AMP_PLAN_SOURCE;
extern NSString *const AMP_PLAN_VERSION;
14 changes: 13 additions & 1 deletion Assets/Plugins/iOS/Amplitude/AMPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
#import "AMPConstants.h"

NSString *const kAMPLibrary = @"amplitude-ios";
NSString *const kAMPVersion = @"7.2.2"; // Version is managed automatically by semantic-release, please don't change it manually
NSString *const kAMPVersion = @"8.5.0"; // Version is managed automatically by semantic-release, please don't change it manually
NSString *const kAMPUnknownLibrary = @"unknown-library";
NSString *const kAMPUnknownVersion = @"unknown-version";
NSString *const kAMPEventLogDomain = @"api2.amplitude.com";
NSString *const kAMPEventLogUrl = @"https://api2.amplitude.com/";
NSString *const kAMPEventLogEuUrl = @"https://api.eu.amplitude.com/";
NSString *const kAMPContentTypeHeader = @"application/x-www-form-urlencoded";
NSString *const kAMPDyanmicConfigUrl = @"https://regionconfig.amplitude.com/";
NSString *const kAMPDyanmicConfigEuUrl = @"https://regionconfig.eu.amplitude.com/";
NSString *const kAMPDefaultInstance = @"$default_instance";
const int kAMPApiVersion = 3;
const int kAMPDBVersion = 3;
Expand All @@ -50,6 +53,11 @@
const int kAMPEventMaxCount = 1000;
NSString *const kAMPPlatform = @"macOS";
NSString *const kAMPOSName = @"macos";
#elif TARGET_OS_WATCH // watchOS, simulator, etc.
const int kAMPEventUploadThreshold = 30;
const int kAMPEventMaxCount = 1000;
NSString *const kAMPPlatform = @"watchOS";
NSString *const kAMPOSName = @"watchos";
#else // iOS, simulator, etc.
const int kAMPEventUploadThreshold = 30;
const int kAMPEventMaxCount = 1000;
Expand Down Expand Up @@ -99,3 +107,7 @@
NSString *const AMP_TRACKING_OPTION_PLATFORM = @"platform";
NSString *const AMP_TRACKING_OPTION_REGION = @"region";
NSString *const AMP_TRACKING_OPTION_VERSION_NAME = @"version_name";

NSString *const AMP_PLAN_BRANCH = @"branch";
NSString *const AMP_PLAN_SOURCE = @"source";
NSString *const AMP_PLAN_VERSION = @"version";
Loading

0 comments on commit 4ea525a

Please sign in to comment.