Corona
Before you integrate the SDK, read Pre-integration preparation.
You can get the Corona SDK source code on GitHub.
The Corona SDK runs on iOS and Android and is about 0.5M in size
Latest version: v2.0.1
Update time: 2023-11-24
Downloads: Source code, SDK download
This document applies to v2.0.0 and later. For earlier versions, see Corona Integration Guide (V1) and SDK download (v1.0.1)
1. Integrate the SDK
-
Add the Corona configuration
- Download the Corona SDK resource files, unzip them, and copy TDAnalytics.lua to the Corona directory
-
Add the Android configuration
- Copy android/ThinkingPluginProxy.java to the android/plugin/src/main/java/plugin/library directory
- Open the Android project in Android Studio, switch the view to Android, add the dependency to build.gradle (Module :plugin), and then click Sync Now to sync
dependencies {
implementation 'cn.thinkingdata.android:ThinkingAnalyticsSDK:3.0.0'
}
- Add code to plugin.library.LuaLoader
If you haven't extended plugin.library.LuaLoader, you can copy android/LuaLoader.java directly to the android/plugin/src/main/java/plugin/library directory to overwrite the existing LuaLoader.java file.
public int invoke(LuaState L) {
NamedJavaFunction[] luaFunctions = new NamedJavaFunction[] {
...
// Code added for ThinkingAnalyticsSDK
new ThinkingBridgingWrapper(),
...
};
String libName = L.toString( 1 );
L.register(libName, luaFunctions);
return 1;
}
public void dispatchEvent(final String message) {
CoronaEnvironment.getCoronaActivity().getRuntimeTaskDispatcher().send( new CoronaRuntimeTask() {
@Override
public void executeUsing(CoronaRuntime runtime) {
LuaState L = runtime.getLuaState();
CoronaLua.newEvent( L, EVENT_NAME );
...
// Code added for ThinkingAnalyticsSDK
JSONObject jsonObject = null;
String newMessage = null;
String type = null;
try {
jsonObject = new JSONObject(message);
newMessage = jsonObject.optString("message");
type = jsonObject.optString("type");
} catch (Exception ignored) {
}
if (!TextUtils.isEmpty(newMessage) && !TextUtils.isEmpty(type)) {
L.pushString(newMessage);
L.setField(-2, "message");
L.pushString(type);
L.setField(-2, "type");
} else {
L.pushString(message);
L.setField(-2, "message");
}
...
try {
CoronaLua.dispatchEvent( L, fListener, 0 );
} catch (Exception ignored) {
}
}
} );
}
...
// Code added for ThinkingAnalyticsSDK
@SuppressWarnings({"WeakerAccess", "SameReturnValue"})
public int thinkingBridging(LuaState L) {
CoronaActivity activity = CoronaEnvironment.getCoronaActivity();
if (activity == null) {
return 0;
}
String word = L.checkString( 1 );
JSONObject params = null;
try {
params = new JSONObject(word);
} catch (JSONException e) {
params = new JSONObject();
}
String method = params.optString("method");
if (method.length() > 0) {
try {
Class aClass = Class.forName("plugin.library.ThinkingPluginProxy");
Method aMethod = aClass.getDeclaredMethod(method, JSONObject.class);
JSONObject jsonObject = (JSONObject) aMethod.invoke(aClass, params);
if (jsonObject != null && jsonObject.getClass() == JSONObject.class && jsonObject.has("message")) {
(new Thread() {
@Override
public void run() {
dispatchEvent(jsonObject.toString());
}
}).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
...
...
// Code added for ThinkingAnalyticsSDK
@SuppressWarnings("unused")
private class ThinkingBridgingWrapper implements NamedJavaFunction {
@Override
public String getName() {
return "thinkingBridging";
}
@Override
public int invoke(LuaState L) {
return thinkingBridging(L);
}
}
...
- Add the iOS configuration
- Open the ios project in Xcode and drag the ThinkingAnalyticsSDK folder from the ios directory into the App directory
- Enable ARC for the project: select TARGETS -> App -> Build Settings -> Objective-C Automatic Reference Counting, and set it to YES
- Disable ARC for some files: select TARGETS -> App -> Build Phases -> Compile Sources -> ThinkingPluginLibrary.mm, and add the -fno-objc-arc flag
2. Initialization
-- Import TDAnalytics
local TDAnalytics = require("TDAnalytics")
-- Initialize TDAnalytics
local params = {
appId = "YOUR_APP_ID",
serverUrl = "YOUR_SERVER_URL"
}
TDAnalytics.init(params)
Note: Because some devices block plaintext transmission by default, we strongly recommend using an HTTPS receiver URL.
3. Common features
3.1 Set the account ID
When a user logs in, you can call login to set the user's account ID. Once the account ID is set, it is used as the user's identifier and is kept until logout is called:
-- Set the account ID
TDAnalytics.login("TA")
Note: This method does not report a user login event.
3.2 Set super properties
Some important properties, such as the player's server and channel, need to be set in every event. In this case, you can set them as super properties. Super properties are properties that every event carries. You can call setSuperProperties() to set super properties. We recommend setting super properties before sending events.
-- Set super properties
TDAnalytics.setSuperProperties({
channel = "App Store",
trip = {"Car", "Train"}
})
Super properties are saved in the cache, so you don't need to set them every time the app starts. If you call setSuperProperties() to upload a super property that was set before, the previous value is overwritten. If a super property has the same Key as a property uploaded through track(), the event's property overwrites the super property.
3.3 Enable auto-tracking
You can enable auto-tracking by configuring the initialization parameters:
-- Enable auto-tracking
-- appInstall = enable auto-tracking of the install event
-- appStart = enable auto-tracking of the start event
-- appEnd = enable auto-tracking of the end event
local params = {
appId = "YOUR_APP_ID",
serverUrl = "YOUR_SERVER_URL",
autoTrack = {
"appStart", "appEnd", "appInstall"
}
}
TDAnalytics.init( params )
Note: If you need a custom distinct ID, make sure to call
identify()to set the distinct ID before enabling auto-tracking.
3.4 Send events
We recommend setting event properties and the conditions for sending events based on the document you prepared earlier. The event name is of string type. It must start with a letter, can contain digits, letters, and underscores "_", can be up to 50 characters long, and is case-insensitive.
TDAnalytics.track( "product_buy", {
product_name="Product Name"
});
- Event properties are of
tabletype, and each element represents one property. - The event property
Keyis the property name and is ofstringtype. It must start with a letter, can contain digits, letters, and underscores "_", can be up to 50 characters long, and is case-insensitive. - Property values support six types: string, number,
bool,Array, object, and object group.
For complex data types, properties can be objects and object groups.
3.5 Set user properties
For general user properties, you can call userSet() to set them. Properties uploaded through this API overwrite the existing values. If the user property doesn't exist yet, it is created.
-- Set user properties
TDAnalytics.userSet({
user_name = "TE"
})
Property format requirements are the same as for event properties.
4. Best practices
The following sample code includes all of the operations above. We recommend using them in the following order:
-- Import TDAnalytics
local TDAnalytics = require("TDAnalytics")
if isPrivacyPolicyAccepted then -- The user has accepted the privacy policy
-- Initialize the SDK
local params = {
appId = "YOUR_APP_ID",
serverUrl = "YOUR_SERVER_URL"
}
TDAnalytics.init(params)
--If the user is logged in, you can set the user's account ID as the unique identifier
TDAnalytics.login("TA")
--After super properties are set, every event carries them
local superProperties = {}
superProperties["channel"] = "ta" -- String
superProperties["age"] = 1 -- Number
superProperties["isSuccess"] = true -- Boolean
superProperties["birthday"] = os.date("%Y-%m-%d %H:%M:%S") -- Time
superProperties["object"] = { key="value" } -- Object
superProperties["object_arr"] = { { key="value" } } -- Object group
superProperties["arr"] = { "value" } -- Array
TDAnalytics.setSuperProperties(superProperties) -- Set super properties
--Send an event
TDAnalytics.track("product_buy", {
product_name="Product Name"
});
--Set user properties
TDAnalytics.userSet({
user_name = "TE"
})
end

