What are we going to build?
In this tutorial, you’ll learn how to build an app that tracks data or user actions via the PhoneGap Google Analytics plugin.
The app will consist of two pages, and will look like:
Before you begin
Tutorial level: intermediate.
Prerequisites: an Appery.io account, and a Google Analytics account with a registered app.
Apache Cordova (PhoneGap)
Apache Cordova (PhoneGap) is automatically included when you create a new project in Appery.io.
Note: To learn more about this component, and any other settings and options, please go to the PhoneGap Google Analytics git page.
Creating a new app
Create a new app in the builder. From the apps page, enter an app name (use any name) and click “Create.”
Creating the UI
Again, the UI contains two pages and looks like:
There is nothing except Button components on both pages.
Lets start with the first page:
- Open the Pages folder. Click the “gear” icon to the right of startScreen, and select “Rename”:
Rename it to boxesScreen. - Place 4 buttons on the page.
- Select the first button, and set its Text property to Open Box. Set it’s Name property to open_box:
- Select second button and set its Text property to Close Box. Set its Name property to close_box.
- Select third button and set its Text property to Go to Jumps. Set its Name property to go_to_jumps.
- Select the fourth button and set its Text property to Exit. Set its Name property to exit.
All the buttons should looks like:
That’s it for first page. You’ll create the second page the same way:
- In the right of the screen, click Create New > Page:
Type jumpsScreen in the opened window, and click “Create Page.” The second page will be created. - Place 4 buttons on the page.
- Select the first button and set its Text property to Jump. Set its Name property to jump:
- Select the second button and set its Text property to Land. Set its Name property to land.
- Select the third button and set its Text property to Go to Boxes. Set its Name property to go_to_boxes.
- Select the the fourth button and set its Text property to Exit. Set its Name property to exit.
All the buttons should looks like:
That’s it for the UI. Now you need to add some JavaScript.
Adding JavaScript code
It’s useful to keep all the needed functions in a separate JavaScript file:
- From Projects view, click Create New > JavaScript, enter name ga, and click “Create JavaScript.” The new JavaScript file will be listed under the JavaScript folder.
- Open the ga.js file, and add following code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344var gaSuccessHandler = function(result) {alert('GA initialized: ' + result);};var gaErrorHandler = function(error) {alert('GA initialization failed: ' + error);};var gaNativePluginResultHandler = function(result) {alert('Event tracked: ' + result);};var gaNativePluginErrorHandler = function(error) {alert('Event tracking failed: ' + error);};var getGAPlugin = function() {if (GAPlugin) {console.log('GA Plugin found');return GAPlugin;}console.log('GA Plugin NOT found');};var initGA = function() {console.log('Initialize GA');if (getGAPlugin()) {getGAPlugin().init(gaSuccessHandler, gaErrorHandler, "ENTER_YOUR_GOOGLE_ANALYTICS_ID_HERE", 10);}};var trackPage = function(pageName) {console.log("trackPage: " + pageName);if (getGAPlugin()) {getGAPlugin().trackPage(gaNativePluginResultHandler, gaNativePluginErrorHandler, pageName);}};var trackEvent = function(category, eventAction, eventLabel, eventValue) {console.log("trackEvent: " + category);if (getGAPlugin()) {getGAPlugin().trackEvent(gaNativePluginResultHandler, gaNativePluginErrorHandler, category, eventAction, eventLabel, eventValue);}}; - Note the string 28. You should use your Google Analytics ID. It can be found on the Google Analytics website, to the right of the created application:
Adding the events
Now you need to add initialize and tracking events to the components:
- Select “boxesScreen.” Open the Events tab, and add the following event:
boxesScreen > Device ready > Run JavaScript. Add the following code:
12initGA();trackPage('boxes'); - Click “Add event.”
- Add one more event directly to the boxesScreen:
boxesScreen > Device ready > Run JavaScript. Add the following code:
1trackPage('boxes'); - Select the “open_box” button. Add the following event:
open_box> Click > Run JavaScript. Add the following code:
1trackEvent("Box", "Open", "times", 1); - Click “Event.”
- Select the “close_box” button. Add the following event: close_box> Click > Run JavaScript. Add the following code:
1trackEvent("Box", "Close", "times", 1); - Click “Add event.”
- Select the “go_to_jumps” button. Add the following event: go_to_jumps> Navigate to page> jumpsScreen.
- Click “Add event.”
- Select the “exit” button. Add the following event: exit > Click > Run JavaScript. Add the following code:
1navigator.app.exitApp();
That’s how all the events on the boxesScreen should look:
Now, add events to the second page. Under the Pages folder, select the jumpsScreen page:
- Open the Events tab, and add the following event:
jumpsScreen > Page show > Run JavaScript. Add the following code:
1trackPage('jumps'); - Click “Add event.”
- Select the “jump” button. Add the following event:
jump > Click > Run JavaScript. Add the following code:
1trackEvent("Jumps", "Jump", "height", 1.5); - Click “Add event.”
- Select land button. Add the following event: land > Click > Run JavaScript. Add the following code:
1trackEvent("Jumps", "Landing", "speed", 12); - Click “Add event.”
- Select the “go_to_boxes” button. Add the following event: go_to_boxes> Navigate to page> boxesScreen.
- Click “Add event.”
- Select the “exit” button. Add the following event: exit > Click > Run JavaScript. Add the following code:
1navigator.app.exitApp();
That’s how all the events on the jumpsScreen should look:
Testing the app
Since we’re invoking a native component, the app needs to be tested as a hybrid app, or installed on the device.
The final app will look like:
Android
Android testing options:
- Use the Appery.io Mobile Tester app. It’s an app that allows you to launch any app created in Appery.io as a hybrid app. You can find it in the Google Play Store.
- Build the Android binary and install it on your device. When the build is completed, you’ll see a QR code. Scanning the QR code will download the app to your phone. You can also email the app to your device.
iOS
iOS options:
- Use the Appery.io Mobile Tester app. It’s an app that allows you to launch any app created in Appery.io as a hybrid app.
- Build the app and install it on your device.