Latest Titanium Count.ly Modules with Messaging Support:
Alloy iOS Android Modules: Count.ly Analytics, Push Notifications – Events, User Profiles, Messaging – Google Analytics Alternative.
Countly is an innovative, real-time, open source mobile analytics application. It collects data from mobile phones, and visualizes this information to analyze mobile application usage and end-user behavior. There are two parts of Countly: the server that collects and analyzes data, and mobile SDK that sends this data (for iOS & Android).
Titanium Countly Messaging Modules
This Titanium iOS and Android module is written to take use of all the Count.ly functions – including events,userData and Messaging.
iOS Installation
- Go to: https://github.com/dieskim/Titanium-Count.ly-Messaging
- Download: count.ly-messaging-iphone-x.x.x.zip
- Move Zip to root of your Application
Android Installation
- Go to: https://github.com/dieskim/Titanium-Count.ly-Android-Messaging
- Download: count.ly-messaging-android-x.x.x.zip
- Move Zip to root of your Application
SETUP Count.ly WITH Messaging – Push
Require the Count.ly Module
var Countly = require('count.ly.messaging');
Set Functions:
// START IF - iOS only
if (Ti.Platform.name == 'iPhone OS'){
// START FUNCTION - registerForPush
function registerForPush() {
Ti.Network.registerForPushNotifications({
success: deviceTokenSuccess,
error: deviceTokenError,
callback: receivePush
});
// Remove event listener once registered for push notifications
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
};
// END FUNCTION - registerForPush
// addEventListener to Wait for user settings to be registered before registering for push notifications
Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
// Start Function - deviceTokenSuccess
function deviceTokenSuccess(e) {
// get Ti.App.Properties - pushSubscribed - to check if already subscribed or not
var pushSubscribed = Ti.App.Properties.getString('pushSubscribed',false);
Ti.API.log('pushSubscribed Value: ' + pushSubscribed);
// START IF - not subscribed then subscribe
if (pushSubscribed != true){
Ti.API.log("Not Subscribed to Count.ly Push - Subscribe with deviceToken: " + e.deviceToken);
// run Count.ly Register Device for Push
Countly.registerDeviceSuccess(e.deviceToken);
// Set Ti.App.Properties push_channels
Ti.App.Properties.setString('pushSubscribed',true);
}else{
Ti.API.log('Already Subscribed to Count.ly Push, wont subscribe again!');
};
// END IF - not subscribed then subscribe
};
// End Function - deviceTokenSuccess
// Start Function - deviceTokenError
function deviceTokenError(e) {
Ti.API.log("Failed to Find Token" + e.error);
// run Count.ly Register registerDeviceError
Countly.registerDeviceError();
};
// End Function - deviceTokenError
//START Countly with Messaging - DEVELOPMENT TEST
Countly.setMessagingDeveloperMode(); // setMessagingDeveloperMode
Countly.startMessagingTest('YOUR_APP_KEY','http://yourserver.com');
//START Countly with Messaging - PRODUCTION
//Countly.startMessaging('YOUR_APP_KEY','http://yourserver.com');
// START FUNCTION - receivePush for iOS
function receivePush(pushMessage) {
// Ti.API.info Raw pushMessage
Ti.API.info("Received Push:" + JSON.stringify(pushMessage));
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// pushMessage EXAMPLE //
// //
// {"code":0, //
// "data":{ "alert":"Test Message", //
// "category":"[CLY]_url", //
// "c":{"l":"http://count.ly","i":"551b9d03f593a55e11ea62c0"}, //
// "sound":"default", //
// "aps":{"category":"[CLY]_url","sound":"default","alert":"test5"} //
// }, //
// "type":"remote", //
// "source":{}, //
// "inBackground":true, //
// "success":true}; //
// //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// pushData
var pushData = pushMessage.data;
// set pushUserInfoDictionary
var pushUserInfoDictionary = {userInfoDictionary: pushMessage.data.c};
// run Count.ly record Push Opened
Countly.recordPushOpen(pushUserInfoDictionary);
// START IF - Set pushAlertMessage
if (pushMessage.data.alert){
var pushAlertMessage = pushMessage.data.alert;
};
// END IF - Set pushAlertMessage
// START IF - Set PUSH CATEGORY TYPE - check more here - http://resources.count.ly/v1.0/docs/countly-push-for-ios
if (pushMessage.data.c.l) {
var pushType = "hasLink";
var pushLink = pushMessage.data.c.l;
///////////////////////////////////////////////////////////
// SHOW AN LINK ALERT HERE //
// 1. Use info - pushType
// - pushLink
// - pushAlertMessage
// 2. Once user Takes action log action with
// Count.ly record Push Action
// Countly.recordPushAction(pushUserInfoDictionary);
//////////////////////////////////////////////////////////
} else if (pushMessage.data.c.r) {
var pushType = "hasReview";
// SHOW AN REVIEW ALERT HERE
} else if (pushMessage.data.c.u) {
var pushType = "hasUpdate";
// SHOW AN UPDATE ALERT HERE
} else {
var pushType = "hasMessage";
// SHOW NORMAL ALERT HERE
};
// END IF - Set PUSH CATEGORY TYPE
};
// END FUNCTION - receivePush for iOS
// ELSE IF - Android only
}else if (Ti.Platform.name == 'android'){
START Countly with Messaging - DEVELOPMENT TEST
// enableDebug if needed
Countly.enableDebug();
// START Countly with Messaging - DEVELOPMENT TEST
Countly.startMessagingTest('COUNLY_APP_KEY','http://yourserver.com','GCM_PROJECT_ID');
START Countly with Messaging - PRODUCTION
// START Countly with Messaging - PRODUCTION
//Countly.startMessaging('COUNLY_APP_KEY','http://yourserver.com','GCM_PROJECT_ID');
// ADD EVENTLISTENTER AND FUNCTION TO MODULE
Countly.addEventListener('receivePush',function(pushMessageData){
// *** IN ANDROID THERE IS NO NEED TO RUN recordPushOpen as it happens Automaticall on the Native Module side **//
// Ti.API.info Raw pushMessage
Ti.API.info("Received Push");
Ti.API.info(JSON.stringify(pushMessageData));
var pushID = pushMessageData.id;
var pushAlertMessage = pushMessageData.message;
var pushType = pushMessageData.type || 'unknownType';
var pushLink = pushMessageData.link || '';
var pushSound = pushMessageData.sound || '';
var pushData = pushMessageData.data; // all message data if needed
Ti.API.info("pushID: " + pushID + " pushAlertMessage: " + pushAlertMessage + "pushType: " + pushType + " pushData: " + pushData + " pushSound: " + pushSound);
if (pushType == "hasLink"){
///////////////////////////////////////////////////////////
// SHOW AN LINK ALERT HERE //
// 1. Use info - pushType
// - pushLink
// - pushAlertMessage
// 2. Once user Takes action log action with recordPushAction using pushID
// Count.ly record Push Action
// Countly.recordPushAction(pushID);
}else if (pushType == "hasReview"){
// SHOW AN REVIEW ALERT HERE
}else if (pushType == "hasMessage"){
// SHOW NORMAL ALERT HERE
};
});
};
// END IF - Android Only
// Countly Set user location
// - Takes two strings: latitudeString and longitudeString of 2 digit lengths
var latitudeString = 12;
var longitudeString = 10;
Countly.setLocation(latitudeString,longitudeString);
//Record Events
//Set any of the following Fields in an Object
var segmentation = {device:"iPhone 4S", country:"USA"};
var eventData = {name: "keySegmentationCountSum", segmentation:segmentation, count: 1, sum: 0.99};
//Track Events Examples
var segmentation = {device:"iPhone 4S", country:"USA"};
Ti.API.log("Send keyCount Event");
var eventData = {name: "keyCount", count: 1};
Countly.event(eventData);
Ti.API.log("Send keyCountSum Event");
var eventData = {name: "keyCountSum", count: 1, sum: 0.99};
Countly.event(eventData);
Ti.API.log("Send keySegmentationCount Event");
var eventData = {name: "keySegmentationCount", segmentation:segmentation, count: 1};
Countly.event(eventData);
Ti.API.log("Send keySegmentationCountSum Event");
var eventData = {name: "keySegmentationCountSum", segmentation:segmentation, count: 1, sum: 0.99};
Countly.event(eventData);
//Set UserData
//Set any of the following Fields in an Object
//*Set userData{} as information about user *Possible keys are:
var userData = { name: "testName",
username: "testUsername",
email: "[email protected]",
organization: "testOrg",
phone: "testPhone",
picture: "https://count.ly/wp-content/uploads/2014/10/logo.png",
picturePath: "/images/appicon.png",
gender: "M",
byear: "1980",
};
//*Set customUserData{} as information about user with custom properties *In customUserData you can provide any string key values to be stored with user
var customUserData = { key1: "value1",
key2:"value2",
};
//*Set Userdata as set in userData and customData *Can contain both userData and customData - or just userdata
Ti.API.log("Set UserData");
var args = { userData:userData,
customUserData:customUserData,
};
Countly.userData(args);
Titanium Alloy iOS Android Modules: Count.ly Analytics, Push Notifications – Events, User Profiles, Messaging – Google Analytics Alternative

One thought on “Titanium Alloy iOS Android Modules: Count.ly Analytics, Push Notifications – Events, User Profiles, Messaging – Google Analytics Alternative”