Apteligent Integration Guide
For the C++ version of cocos2d-x v3.x - (all other versions)
SDK Version
- ios: v5.7.3
- android: 5.8.10
Integration
Sign up for Apteligent and create your app.
Open a terminal and use the following command to install the SDKBOX Apteligent plugin.
$ sdkbox import apteligent
Important Notice
Please make sure the following settings in your project to make the plugin work well.
Disable App Transport Security
Adding the following entry to the info.plist
file:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
It should look like this:
Disable Bitcode support
You have to turn off Bitcode support. If you don't, cocos2d-x will fail to build.
Set your game requires full screen
If your game doesn't support all screen orientations, you will need to check Requires full screen
in Xcode. If you do not, your app will fail Apple's submission process.
Whitelist canOpenURL function
This setting depends on what plugins are in your project. You may need to add the required entry to the info.plist
, under LSApplicationQueriesSchemes
.
JSON Configuration
SDKBOX Installer will automatically inject a sample configuration to your sdkbox_config.json
, that you have to modify it before you can use it for your own app
- id: Apteligent App ID
options
-
monitor_connection [bool]: (iOS only)
- true [default]
- false
Determines whether Service Monitoring should capture network performance information for network calls made through NSURLConnection.
-
monitor_session [bool]: (iOS only)
- true [default]
- false
Determines whether Service Monitoring should capture network performance information for network calls made through NSURLSession.
-
enable_service_monitoring [bool]:
- true [default]
- false
This flag determines wither Apteligent service monitoring is enabled at all.
-
delay_sending_appload [bool]:
- true
- false [default]
Delay to reports an app load event.
-
url_filters [array]:
- ["http://www.gmail.com", "www.other.com"]
These filters are used to make it so certain network performance information is not reported to Apteligent.
-
logcat_reporting_enabled: (Android only) Include Logcat
- true
- false [default]
Including system log data (Logcat) can be helpful for debugging crashes and handled exceptions, but it comes at the cost of slightly increasing disk and network usage, which is why this feature must be manually enabled. Apteligent collects the last 100 lines of logcat data.
more information:
Example:
{
"ios": {
"Apteligent":{
"debug":true,
"id":"956194d922984692a3184816ec3a510300555300",
"monitor_connection":true,
"monitor_session":true,
"enable_service_monitoring":true,
"delay_sending_appload":false,
"url_filters":[
"https://metrics.sdkbox.com",
"http://www.gmail.com"
]
}
},
"android": {
"Apteligent":{
"debug":true,
"id":"3f66168979494ce38374b4d32d637dcc00555300",
"enable_service_monitoring":true,
"delay_sending_appload":false,
"url_filters":[
"https://metrics.sdkbox.com",
"http://www.gmail.com"
],
"version":"version:2.0.4 build:4981 codeName:Cactus",
"version_string_include_version_code":true,
"logcat_reporting_enabled":true
}
}
}
Usage
Initialize Apteligent
Initialize the plugin where appropriate in your code. We recommend to do this in the AppDelegate::applicationDidFinishLaunching()
or AppController:didFinishLaunchingWithOptions()
. Make sure to include the appropriate headers. Example:
#include "PluginApteligent/PluginApteligent.h"
AppDelegate::applicationDidFinishLaunching()
{
sdkbox::PluginApteligent::init();
}
Logging Breadcrumbs
// leave a breadcrumb
sdkbox::PluginApteligent::leaveBreadcrumb("User tapped start button");
// perform all breadcrumb writes on a background thread
sdkbox::PluginApteligent::setAsyncBreadcrumbMode(true);
Logging User Metadata
// Adding a Username
sdkbox::PluginApteligent::setUsername("MrsCritter");
// Adding Arbitrary User Metadata
sdkbox::PluginApteligent::setValueforKey("5", "Game Level");
Logging Userflows
// Beginning a Userflow
sdkbox::PluginApteligent::beginUserflow("login");
// Ending a Userflow
sdkbox::PluginApteligent::endUserflow("login");
// Failing a Userflow
sdkbox::PluginApteligent::failUserflow("login");
// Cancelling a Userflow
sdkbox::PluginApteligent::cancelUserflow("login");
Modifying the Value of a Userflow
int valueInCents = sdkbox::PluginApteligent::valueForUserflow("my_userflow");
valueInCents += 5;
sdkbox::PluginApteligent::setValueforUserflow(valueInCents, "my_userflow");
Logging Network Requests
sdkbox::PluginApteligent::logNetworkRequest("GET", "http://www.abc123def456.com", 2.0, 1000, 100, 200);
Filtering Captured Data
sdkbox::PluginApteligent::addFilter("www.gmail.com");
Configuring Location
// Beijing, China
sdkbox::PluginApteligent::updateLocation(39.9042, 116.4074);
Other Tasks
// Allowing Users to Opt Out of Apteligent
sdkbox::PluginApteligent::setOptOutStatus(true);
// Changing the verbosity of Apteligent's Logs
// sdkbox::CRLoggingLevelSilent : Turns off all Apteligent log messages
// sdkbox::CRLoggingLevelError : Only print errors. An error is an unexpected event that will result not capturing important data
// sdkbox::CRLoggingLevelWarning : (Default) Only print warnings. Currently warning messages are printed when calling Apteligent methods before initializing Apteligent.
// sdkbox::CRLoggingLevelInfo : The most verbose level of logging
sdkbox::PluginApteligent::setLoggingLevel(sdkbox::CRLoggingLevelInfo);
// Send App Load Data
// You must set `"delay_sending_appload":true` in `sdkbox_config.json` first
sdkbox::PluginApteligent::sendAppLoadData();
Implement ApteligentListener
- You can implement ApteligentListener if you want to receive callbacks like video finish playing.
#include "PluginApteligent/PluginApteligent.h"
class MyClass : public sdkbox::ApteligentListener
{
private:
void onCrashOnLastLoad() {} // not support on android
}
API Reference
Methods
static bool init ( ) ;
initialize the plugin instance.
static void setListener ( ApteligentListener * listener ) ;
Set listener to listen for apteligent events
static ApteligentListener * getListener ( ) ;
Get the listener
static void removeListener ( ) ;
Remove the listener, and can't listen to events anymore
static void addFilter ( const std::string & filter ) ;
Adds an additional filter for network instrumentation.
static void leaveBreadcrumb ( const std::string & breadcrumb ) ;
Breadcrumbs provide the ability to track activity within your app.
A breadcrumb is a free form string you supply, which will be timestamped, and stored in case a crash occurs. Crash reports will contain the breadcrumb trail from the run of your app that crashed, as well as that of the prior run. Breadcrumbs are limited to 140 characters, and only the most recent 100 are kept. Apteligent will automatically insert a breadcrumb marked "session_start" on each initial launch, or foreground of your app. Note - Breadcrumbs are an Enterprise level feature.
static void setAsyncBreadcrumbMode ( bool writeAsync ) ;
By default, breadcrumbs are flushed to disk immediately when written. This is by design - in order to provide an accurate record of everything that happened up until the point your app crashed.
If you are concerned about the performance costs of writing the file, you can instruct the library to perform all breadcrumb writes on a background thread.
static void updateLocation ( double latitude , double longitude ) ;
Inform Apteligent of the device's most recent location for use with performance monitoring.
static bool logNetworkRequest ( const std::string & method ,
const std::string & urlString ,
long latency ,
long bytesRead ,
long bytesSent ,
long responseCode ) ;
Logging endpoints is a way of manually logging custom network library network access to URL's which fall outside Apteligent's monitoring of NSURLConnection and ASIHTTPRequest method calls.
static void setOptOutStatus ( bool status ) ;
If you wish to offer your users the ability to opt out of Apteligent crash reporting, you can set the OptOutStatus to YES. If you do so, any pending crash reports will be purged.
static bool getOptOutStatus ( ) ;
Retrieve current opt out status.
static void setMaxOfflineCrashReports ( int max ) ;
Set the maximum number of crash reports that will be stored on the local device if the device does not have internet connectivity. If more than |maxOfflineCrashReports| crashes occur, the oldest crash will be overwritten. Decreasing the value of this setting will not delete any offline crash reports. Unsent crash reports will be kept until they are successfully transmitted to Apteligent, hence there may be more than |maxOfflineCrashReports| stored on the device for a short period of time.
The default value is 3, and there is an upper bound of 10.
static int maxOfflineCrashReports ( ) ;
Get the maximum number of Apteligent crash reports that will be stored on the local device if the device does not have internet connectivity.
static std::string getUserUUID ( ) ;
Get the Apteligent generated unique identifier for this device.
!! This is NOT the device's UDID. If called before enabling the library, this will return an empty string. All Apteligent enabled apps on a device will share the UUID created by the first installed Apteligent enabled app. If all Apteligent enabled applications are removed from a device, a new UUID will be generated when the next one is installed.
static void setUsername ( const std::string & username ) ;
Associate a username string with the device's Apteligent UUID. This will send the association to Apteligent's back end.
static void setValueforKey ( const std::string & value ,
const std::string & key ) ;
Associate an arbitrary key/value pair with the device's Apteligent UUID.
static bool didCrashOnLastLoad ( ) ;
Did the application crash on the previous load?
static void beginUserflow ( const std::string & name ) ;
Init and begin a userflow with a default value.
static void beginUserflow ( const std::string & name , int value ) ;
Init and begin a userflow with an input value.
static void cancelUserflow ( const std::string & name ) ;
Cancel a userflow as if it never existed.
static void endUserflow ( const std::string & name ) ;
End an already begun userflow successfully.
static void failUserflow ( const std::string & name ) ;
End an already begun userflow as a failure.
static int valueForUserflow ( const std::string & name ) ;
Get the currency cents value of a userflow.
static void setValueforUserflow ( int value , const std::string & name ) ;
Set the currency cents value of a userflow.
static void sendAppLoadData ( ) ;
Tell Apteligent to send app load event. By default, Apteligent will send app load event automatically when your app is started However, if you set delaySendingAppLoad flag to YES on config, you can call this method to manually send app load event.
static void setLoggingLevel ( int loggingLevel ) ;
Set the logging level to tune the verbosity of Apteligent log messages.
Listeners
void onCrashOnLastLoad ( )
Notifies the delegate that the app has crash last time.
Manual Integration
If the SDKBOX Installer fails to complete successfully, it is possible to integrate SDKBOX manually. If the installer complete successfully, please do not complete anymore of this document. It is not necessary.
These steps are listed last in this document on purpose as they are seldom needed. If you find yourself using these steps, please, after completing, double back and re-read the steps above for other integration items.
Manual Integration For iOS
Drag and drop the following frameworks from the plugins/ios folder of the Apteligent
bundle into your Xcode project, check Copy items if needed
when
adding frameworks:
sdkbox.framework
PluginApteligent.framework
Crittercism.framework
You also need to add the following system frameworks, if you don't already have them:
SystemConfiguration.framework
CoreLocation.framework
Manual Integration For Android
SDKBOX supports three different kinds of Android projects command-line, eclipse and Android Studio.
proj.android
will be used as our<project_root>
for command-line and eclipse projectproj.android-studio
will be used as our<project_root>
for Android Studio project.
Copy Files
Copy the everything from plugin/android/libs
folder of this
bundle into your project's
-
If you're using cocos2d-x from source copy the jar files to:
Android command-line:
cocos2d/cocos/platform/android/java/libs
Android Studio:
cocos2d/cocos/platform/android/java/libs cocos2d/cocos/platform/android/libcocos2dx/libs
-
If you're using cocos2d-js or lua copy the jar files to:
Android command-line:
frameworks/cocos2d-x/cocos/platform/android/java/libs
Android Studio:
frameworks/cocos2d-x/cocos/platform/android/java/libs frameworks/cocos2d-x/cocos/platform/android/libcocos2dx/libs
-
If you're using prebuilt cocos2d-x copy the jar files to:
Android command-line:
proj.android/libs
Copy jni libs
Copy and overwrite all the folders from plugin/android/jni
to your <project_root>/jni/
directory.
Edit AndroidManifest.xml
Include the following permissions above the application tag:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
Edit Android.mk
Edit <project_root>/jni/Android.mk
to:
Add additional requirements to LOCAL_WHOLE_STATIC_LIBRARIES:
LOCAL_WHOLE_STATIC_LIBRARIES += PluginApteligent
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox
Add a call to:
$(call import-add-path,$(LOCAL_PATH))
before any import-module statements.
Add additional import-module statements at the end:
$(call import-module, ./sdkbox)
$(call import-module, ./pluginapteligent)
This means that your ordering should look similar to this:
$(call import-add-path,$(LOCAL_PATH))
$(call import-module, ./sdkbox)
$(call import-module, ./pluginapteligent)
Note: It is important to make sure these statements are above the existing $(call import-module,./prebuilt-mk)
statement, if you are using the pre-built libraries.
Modify Application.mk
(Cocos2d-x v3.0 to v3.2 only)
Edit <project_root>/jni/Application.mk
to make sure APP_STL is defined
correctly. If Application.mk
contains APP_STL := c++_static
, it should be
changed to:
APP_STL := gnustl_static
Modify AppActivity.java
Plugin >= 2.4.0.3
- Find the AppActivity.java
find . -name "AppActivity.java"
- Replace
extends Cocos2dxActivity
withextends com.sdkbox.plugin.SDKBoxActivity
Example of the directory where the AppActivity.java file is located:
cpp
- proj.android/src/org/cocos2dx/cpp/AppActivity.java
- proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java
- proj.android/app/src/org/cocos2dx/cpp/AppActivity.java ( from cocos2d-x 3.17)
lua
- frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android-studio/app/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/lua/AppActivity.java (from cocos2d-x 3.17)
js
- frameworks/runtime-src/proj.android/src/org/cocos2dx/javascript/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/javascript/AppActivity.java ( from cocos2d-x 3.17)
Plugin < 2.4.0.3
-
If you're using cocos2d-x from source, assuming you are in the proj.android directory, Cocos2dxActivity.java is located:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/ lib/Cocos2dxActivity.java
-
If you're using the prebuilt cocos2d-x libraries assuming you are in the
proj.android
directory, Cocos2dxActivity.java is located:./src/org/cocos2dx/lib/Cocos2dxActivity.java
Note: When using Cocos2d-x from source, different versions have Cocos2dxActivity.java in a different location. One way to find the location is to look in proj.android/project.properties. Example:
android.library.reference.1=../../cocos2d-x/cocos/platform/android/java
In this case, Cocos2dxActivity.java should be located at:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
- Modify Cocos2dxActivity.java to add the following imports:
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
- Second, modify Cocos2dxActivity.java to edit the
onCreate(final Bundle savedInstanceState)
function to add a call toSDKBox.init(this);
. The placement of this call is important. It must be done after the call toonLoadNativeLibraries();
. Example:
onLoadNativeLibraries();
SDKBox.init(this);
-
Last, we need to insert the proper overrides code. There are a few rules here.
-
If the method listed has not been defined, add it.
-
If the method listed has been defined, add the calls to
SDKBox
in the same existing function.
-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!SDKBox.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onStart() {
super.onStart();
SDKBox.onStart();
}
@Override
protected void onStop() {
super.onStop();
SDKBox.onStop();
}
@Override
protected void onResume() {
super.onResume();
SDKBox.onResume();
}
@Override
protected void onPause() {
super.onPause();
SDKBox.onPause();
}
@Override
public void onBackPressed() {
if(!SDKBox.onBackPressed()) {
super.onBackPressed();
}
}
Proguard (optional)
- Edit
project.properties
to specify aProguard
configuration file. Example:
proguard.config=proguard.cfg
- Edit the file you specified to include the following:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# cocos2d-x
-keep public class org.cocos2dx.** { *; }
-dontwarn org.cocos2dx.**
-keep public class com.chukong.** { *; }
-dontwarn com.chukong.**
# google play service
-keep public class com.google.android.gms.** { public *; }
-dontwarn com.google.android.gms.**
-keep class com.google.protobuf.** { *; }
-dontwarn com.google.protobuf.**
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
#sdkbox
-keep public class com.sdkbox.** { *; }
-dontwarn com.sdkbox.**
# apteligent
-keep public class android.webkit.** { *; }
-dontwarn android.webkit.**
-keep public class com.crittercism.**
-keepclassmembers public class com.crittercism.*
{
*;
}
Note: Proguard only works with Release builds (i.e cocos run -m release
) debug builds do not invoke Proguard rules.