ProtocolAnalytics.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /****************************************************************************
  2. Copyright (c) 2012+2013 cocos2d+x.org
  3. http://www.cocos2d+x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "ProtocolAnalytics.h"
  21. #import "InterfaceAnalytics.h"
  22. #include "PluginUtilsIOS.h"
  23. namespace cocos2d { namespace plugin {
  24. ProtocolAnalytics::ProtocolAnalytics()
  25. {
  26. }
  27. ProtocolAnalytics::~ProtocolAnalytics()
  28. {
  29. PluginUtilsIOS::erasePluginOCData(this);
  30. }
  31. /**
  32. @brief Start a new session.
  33. @param appKey The identity of the application.
  34. */
  35. void ProtocolAnalytics::startSession(const char* appKey)
  36. {
  37. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  38. assert(pData != NULL);
  39. id ocObj = pData->obj;
  40. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  41. NSObject<InterfaceAnalytics>* curObj = ocObj;
  42. NSString* pStrKey = [NSString stringWithUTF8String:appKey];
  43. [curObj startSession:pStrKey];
  44. }
  45. }
  46. /**
  47. @brief Stop a session.
  48. @warning This interface only worked on android
  49. */
  50. void ProtocolAnalytics::stopSession()
  51. {
  52. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  53. assert(pData != NULL);
  54. id ocObj = pData->obj;
  55. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  56. NSObject<InterfaceAnalytics>* curObj = ocObj;
  57. [curObj stopSession];
  58. }
  59. }
  60. /**
  61. @brief Set the timeout for expiring a session.
  62. @param millis In milliseconds as the unit of time.
  63. @note It must be invoked before calling startSession.
  64. */
  65. void ProtocolAnalytics::setSessionContinueMillis(long millis)
  66. {
  67. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  68. assert(pData != NULL);
  69. id ocObj = pData->obj;
  70. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  71. NSObject<InterfaceAnalytics>* curObj = ocObj;
  72. [curObj setSessionContinueMillis:millis];
  73. }
  74. }
  75. /**
  76. @brief log an error
  77. @param errorId The identity of error
  78. @param message Extern message for the error
  79. */
  80. void ProtocolAnalytics::logError(const char* errorId, const char* message)
  81. {
  82. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  83. assert(pData != NULL);
  84. id ocObj = pData->obj;
  85. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  86. NSObject<InterfaceAnalytics>* curObj = ocObj;
  87. NSString* pError = [NSString stringWithUTF8String:errorId];
  88. NSString* pMsg = [NSString stringWithUTF8String:message];
  89. [curObj logError:pError withMsg:pMsg];
  90. }
  91. }
  92. /**
  93. @brief log an event.
  94. @param eventId The identity of event
  95. @param paramMap Extern parameters of the event, use NULL if not needed.
  96. */
  97. void ProtocolAnalytics::logEvent(const char* eventId, LogEventParamMap* paramMap)
  98. {
  99. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  100. assert(pData != NULL);
  101. id ocObj = pData->obj;
  102. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  103. NSObject<InterfaceAnalytics>* curObj = ocObj;
  104. NSString* pId = [NSString stringWithUTF8String:eventId];
  105. NSMutableDictionary* dict = PluginUtilsIOS::createDictFromMap(paramMap);
  106. [curObj logEvent:pId withParam:dict];
  107. }
  108. }
  109. /**
  110. @brief Track an event begin.
  111. @param eventId The identity of event
  112. */
  113. void ProtocolAnalytics::logTimedEventBegin(const char* eventId)
  114. {
  115. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  116. assert(pData != NULL);
  117. id ocObj = pData->obj;
  118. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  119. NSObject<InterfaceAnalytics>* curObj = ocObj;
  120. NSString* pEvent = [NSString stringWithUTF8String:eventId];
  121. [curObj logTimedEventBegin:pEvent];
  122. }
  123. }
  124. /**
  125. @brief Track an event end.
  126. @param eventId The identity of event
  127. */
  128. void ProtocolAnalytics::logTimedEventEnd(const char* eventId)
  129. {
  130. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  131. assert(pData != NULL);
  132. id ocObj = pData->obj;
  133. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  134. NSObject<InterfaceAnalytics>* curObj = ocObj;
  135. NSString* pEvent = [NSString stringWithUTF8String:eventId];
  136. [curObj logTimedEventEnd:pEvent];
  137. }
  138. }
  139. /**
  140. @brief Whether to catch uncaught exceptions to server.
  141. @warning This interface only worked on android.
  142. */
  143. void ProtocolAnalytics::setCaptureUncaughtException(bool enabled)
  144. {
  145. PluginOCData* pData = PluginUtilsIOS::getPluginOCData(this);
  146. assert(pData != NULL);
  147. id ocObj = pData->obj;
  148. if ([ocObj conformsToProtocol:@protocol(InterfaceAnalytics)]) {
  149. NSObject<InterfaceAnalytics>* curObj = ocObj;
  150. [curObj setCaptureUncaughtException:enabled];
  151. }
  152. }
  153. }} //namespace cocos2d { namespace plugin {