寫在前面:年前的時候,極光社區組織了一場徵文活動 ,收到很多好的文章。如今打算和你們一塊兒分享一下這些優秀的做品 :)javascript
做者:Wilhan - 極光html
原文:在 cordova 中使用極光統計服務java
cordova-plugin-janalytics
的開發過程, 爲cordova和極光新手提供幫助。cordova-plugin-janalytics
?cordova plugin install cordova-plugin-janalytics --variable API_KEY=極光KEY --variable CHANNEL=渠道名
Janalytics.onPageStart("登錄頁面", function(){}, function(err){});
更多更詳細的用法請移步到cordova-plugin-janalyticsandroid
plugman create --name jcore --plugin_id cordova-plugin-jcore --plugin_version 0.0.1
plugman platform add -platform_name android
plugman platform add -platform_name ios
經過官方文檔得知,極光統計SDK須要兩個配置參數APP_KEY和CHANNEL
其中APP_KEY極光分配的應用標示,CHANNEL是做爲渠道標示
爲了插件在多個項目中複用,故將APP_KEY和CHANNEL做爲變量ios
<?xml version='1.0' encoding='utf-8'?> <plugin id="cordova-plugin-janalytics" version="1.0.4" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"> <name>JAnalytics</name> <description>JAnalytics for cordova plugin</description> <author>WilhanTian</author> <keywords>JAnalytics,analytics</keywords> <license>MIT License</license> <preference name="APP_KEY"/><!--應用ID--> <preference name="CHANNEL" default="DEFAULT_CHANNEL"/><!--渠道字符串 注意!禁止爲純數字--> <dependency id="cordova-plugin-jcore" version="1.0.1"/> <!--cordova引擎版本限制--> <engines> <engine name="cordova" version=">=3.0" /> </engines> <js-module name="JAnalytics" src="www/JAnalytics.js"> <clobbers target="JAnalytics" /> </js-module> <!--IOS--> <platform name="ios"> <config-file parent="/*" target="config.xml"> <feature name="JAnalytics"> <param name="ios-package" value="JAnalytics" /> </feature> </config-file> <config-file parent="JAnalytics" target="*-Info.plist"> <dict> <key>APP_KEY</key> <string>$APP_KEY</string> <key>CHANNEL</key> <string>$CHANNEL</string> </dict> </config-file> <!--庫依賴--> <framework src="UIKit.framework" weak="true" /> <framework src="SystemConfiguration.framework" weak="true" /> <framework src="CoreTelephony.framework" weak="true" /> <framework src="CoreGraphics.framework" weak="true" /> <framework src="Security.framework" weak="true" /> <framework src="Foundation.framework" weak="true" /> <framework src="CoreLocation.framework" weak="true" /> <framework src="CoreFoundation.framework" weak="true" /> <framework src="CFNetwork.framework" weak="true" /> <framework src="libz.tbd" weak="true" /> <framework src="libresolv.tbd" weak="true" /> <!--庫文件--> <header-file src="src/ios/libs/JANALYTICSService.h" /> <header-file src="src/ios/libs/JANALYTICSEventObject.h" /> <source-file src="src/ios/libs/janalytics-ios-1.0.0.a" framework="true" /> <!--IOS插件源碼--> <header-file src="src/ios/JAnalytics.h" /> <source-file src="src/ios/JAnalytics.m" /> </platform> <!--Android--> <platform name="android"> <config-file parent="/*" target="res/xml/config.xml"> <feature name="JAnalytics"> <param name="android-package" value="org.cordova.plugin.janalytics.JAnalytics" /> </feature> </config-file> <config-file parent="/*" target="AndroidManifest.xml"/> <!--權限--> <config-file parent="/manifest" target="AndroidManifest.xml"> <uses-permission android:name="android.permission.RECEIVER_USER_PRESENT" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> </config-file> <!--基礎配置--> <config-file parent="/manifest/application" target="AndroidManifest.xml"> <meta-data android:name="JPUSH_APPKEY" android:value="$APP_KEY"/> <meta-data android:name="JPUSH_CHANNEL" android:value="$CHANNEL"/> </config-file> <!--jar包--> <source-file src="src/android/libs/janalytics-android_v1.0.0.jar" target-dir="libs" /> <!--java源碼--> <source-file src="src/android/JAnalytics.java" target-dir="src/org/cordova/plugin/janalytics" /> </platform> </plugin>
標籤解析:git
plugin plugin install 插件名 --variable 變量名=變量
注入變量。.mm
.m
.cpp
.java
和.jar
等文件均可以使用此標籤引入。使用plugman建立工程後,已經默認幫你建立了一個java和oc類,只須要兩個類中撰寫相關業務便可。github
Android示例apache
public class JAnalytics extends CordovaPlugin { @Override protected void pluginInitialize() { //初始化極光統計服務 JAnalyticsInterface.init(this.cordova.getActivity().getApplication()); } @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if(action.equals("onPageStart")){//判斷動做名 String pageName = args.getString(0);//從json數組中取出第一個參數 JAnalyticsInterface.onPageStart(this.cordova.getActivity(), pageName);//向統計一個頁面進入事件 callbackContext.success();//反饋給js回調函數 } return true; } }
IOS實例json
@implementation JAnalytics - (void)pluginInitialize { // 從plist中讀取變量 NSDictionary *plistDic = [[NSBundle mainBundle] infoDictionary]; NSString* APP_KEY = [[plistDic objectForKey:@"JAnalytics"] objectForKey:@"APP_KEY"]; NSString* CHANNEL = [[plistDic objectForKey:@"JAnalytics"] objectForKey:@"CHANNEL"]; // 初始化極光統計服務 JANALYTICSLaunchConfig * config = [[JANALYTICSLaunchConfig alloc] init]; config.appKey = APP_KEY; config.channel = CHANNEL; [JANALYTICSService setupWithConfig:config]; } // js的動做名會自動映射到oc方法名上 - (void)onPageStart:(CDVInvokedUrlCommand *)command { NSString* pageName = [command.arguments objectAtIndex:0];//從字典中取出第一個參數 [JANALYTICSService startLogPageView:pageName];//向統計一個頁面進入事件 //反饋給js回調函數 CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } @end
www/JAnalytics.jsapi
示例
var exec = require('cordova/exec'); // 頁面啓動接口 exports.onPageStart = function(pageName, success, error) { pageName = pageName || ""; exec(success, error, "JAnalytics", "onPageStart", [pageName]); };
若是 cordova-plugin-janalytics 幫助了你,那就請給個 Star 吧;若是發現有 bug 或者有意見,歡迎在 github 中 PR 和 issues。
歡迎關注知乎專欄:極光日報