问题
I am using phonegap 3.3 to build android app locally and trying to use the GA plugin
I have installed the GAplugin using
phonegap local plugin add https://github.com/phonegap-build/GAPlugin.git
Added this code in my index.
var gaPlugin;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("calling ga");
gaPlugin = window.plugins.gaPlugin;
alert("calling init")
gaPlugin.init(successHandler, errorHandler, "UA-48220634-1", 1);
gaPlugin.trackPage( nativePluginResultHandler, nativePluginErrorHandler, "index.html");
gaPlugin.trackEvent( nativePluginResultHandler, nativePluginErrorHandler, "Button", "Click", "event only", 1);
gaPlugin.exit(nativePluginResultHandler, nativePluginErrorHandler);
}
function successHandler()
{
alert("init success");
}
function errorHandler()
{
alert("init failed");
}
function nativePluginResultHandler()
{
alert("tracking success");
}
function nativePluginErrorHandler()
{
alert("tracking failed");
}
I get all successful messages but cant see anything in Google Analytics.
I also tried manually including the GAPlugin.js but no change.
I can see GAPlugin in list of plugins
Anujs-MacBook-Pro:my-app edunewz$ phonegap plugin list
[phonegap] com.adobe.plugins.GAPlugin
[phonegap] com.jamiestarke.webviewdebug
[phonegap] org.apache.cordova.camera
[phonegap] org.apache.cordova.file
[phonegap] org.apache.cordova.file-transfer
[phonegap] org.apache.cordova.inappbrowser
Can anybody help me out.
回答1:
Go into /platforms/android/AndroidManifest.xml and make sure
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
are present.
Execute:
phonegap local plugin add https://github.com/phonegap-build/GAPlugin.git
phonegap local plugin add https://github.com/apache/cordova-plugin-network-information.git
phonegap local plugin add https://github.com/apache/cordova-plugin-geolocation.git
I also added:
<gap:plugin name="com.adobe.plugins.gaplugin" />
<gap:plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
<gap:plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />
to config.xml.
Make sure you copy + paste GAPlugin.js from /plugins/com.adobe.plugins.GAPlugin/www into your /www folder.
The code posted below comes from: https://github.com/shanabus/phonegapbuildtest/blob/master/Index.html
Most of it is just extra, as he shows working feedback in the actual app.
<script src="GAPlugin.js"></script>
<script type="text/javascript" charset="utf-8">
//var baseUrl = 'http://grappsmobile.com/grtv/';
$(function() {
// on exit
// gaPlugin.exit(nativePluginResultHandler, nativePluginErrorHandler);
});
var gaPlugin;
document.addEventListener("deviceready", myDeviceReadyListener, false);
function myDeviceReadyListener() {
gaPlugin = window.plugins.gaPlugin;
gaPlugin.init(gaSuccess, gaError, "UA-23823097-4", 10);
//23823097-4 <-- web property
//23823097-3 <-- app property
}
function gaSuccess() {
try
{
gaPlugin.trackPage( nativePluginResultHandler, nativePluginErrorHandler, "index.html");
$("#gaResults").html("<span class='success'>GA tracked pageview!</span>");
$("#pageHome ul li a").off("click").on("click", function() {
gaPlugin.trackPage( nativePluginResultHandler, nativePluginErrorHandler, $(this).attr("href") );
});
} catch (e) {
$("#gaResults").html("<span class='error'>GA could not track pageview - " + e + "</span>");
}
}
function gaError() {
$("#gaResults").append("<span class='error'>GA error</span>");
}
function nativePluginResultHandler(obj) {
$("#gaResults").append("<br /><span class='warn'>GA Plugin Result - ");
if (obj != undefined) {
$("#gaResults").append(obj);
}
$("#gaResults").append("</span>");
}
function nativePluginErrorHandler() {
$("#gaResults").append("<span class='error'>GA Plugin Error Result</span>");
}
</script>
Make sure you're using Mobile App Analytics, and not web analytics like he is in the above code snippet.
Observe how he is tracking these events (I learned a lot from this): https://github.com/shanabus/phonegapbuildtest/search?q=gaPlugin.trackEvent
Use the plugin's example for how to accomplish VariableButtonClicked, PageButtonClicked, TrackButtonClicked. https://github.com/bobeast/GAPlugin/blob/master/Example/index.html The example code never worked out of the box for me, so just use it as a reference.
Check under https://www.google.com/analytics/web/?hl=en#realtime/ and pray. Sometimes it takes a long time before showing, so give it a day before you start troubleshooting. The rest of analytics data is like 24 hours behind, so select the current day if you want to see data 1-5 hours after.. sometimes longer.
Some people might say these extra steps are overkill, and they would be right.. but I had to do some combination of all of them before it worked for me. Strangely enough I feel like I'm leaving something out. Good luck!
回答2:
in My Case it was network information plugin missing :
cordova plugin add org.apache.cordova.network-information
来源:https://stackoverflow.com/questions/21902799/phonegap-ga-plugin-not-working