---
title: "Sample Application Guide"
slug: "sample-application-guide"
updated: 2024-06-12T18:22:26Z
published: 2024-06-12T18:22:26Z
---

> ## Documentation Index
> Fetch the complete documentation index at: https://onpremises.webtrends.help/llms.txt
> Use this file to discover all available pages before exploring further.

# Sample Application Guide

The sample applications that ship with the Android Analytics SDK are designed to give you, the developer, real-world examples of how to use the libraries.

The app shows off how to use the Analytics parts of the SDK with your app.

## Setting Up the Sample App
The sample applications can be found [Here](https://cdn.webtrends.com/downloads/sdk_android/webtrends_android_sdk_3.3.5.zip){target="_blank"}. Within this zip you will find the `WTSampleApp` folder.


1. **Download the application** to your local machine. 
2. **Ensure you have the latest Android Studio installed** on your system. For more information on installing and using Android Studio, see "Instrumenting an Android Application"  for details.
3. **Install JDK 1.7 or above** in your local machine and make it available for Android Studio by setting under *File -> Project Structure -> SDK location -> JDK* location
4. **Import Sample App Project** into the Android Studio under *Import project ->your-folder-path/mobile-sdk-sample-apps/WTSampleApp*
5. Install the Android Webtrends SDK from the Android Studio under *File -> Project Structure -> SDK location-> New Module -> Import .JAR/.AAR Package*
6. **Resolve module dependency** Add module dependency to the sample app under *File -> Project Structure -> Modules -> com.webtrends.android* and click the **Dependencies** tab on the right to add **webtrends-android-sdk-release-lib-3.0** module in the list.
7. **Compile and run** the app on a simulator or a physical device.


## A Walkthrough of the App
Open the application and tap the **Sign In** button. It is not necessary to use a login ID or password. These fields are just dummies.

![sampleApp1](https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleApp1.png){height="" width="300px"}

This is the main menu Home page. From here, you can navigate to anywhere in the app using the buttons on the main view or the tab bar buttons along the bottom. Tap the **Transactions** button.

![sampleApp2](https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleApp2.png){height="" width="300px"}

Here you can type something in the search bar and tap **enter**. This will trigger a dummy search method. The method does not actually search for anything, but it does trigger a popup to indicate that a search event has occurred. The search method makes a call to the SDK to trigger a new Analytics search event.

<p float="left"> 
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppSearch1.png" width="300px"> &nbsp &nbsp &nbsp &nbsp &nbsp
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppSearch2.png" width="300px">
</p>    

Peeking at the code in the `TransactionsFragment` class shows exactly what is going on here:

```Java
@Override
public boolean onQueryTextSubmit(String queryText) {
if (searchView != null) {
   // customData parameter.
   // Specifies a series of custom name-value parameters used to pass data that is not included in the method.       
   Map<String, String> customData = new HashMap<String, String>();
   customData.put("key1", "value1");
   customData.put("key2", "value2");
   customData.put("key3", "value3");
   // Send conversion event to data collector
   // See details at: http://help.webtrends.com/en/android/#mobile_event_collection_methods.html
   wtDataCollector.onSearchEvent(eventPath, eventDesc, eventType, customData, searchPhrase, searchResurt);
   final InputMethodManager imm=(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
   if (imm != null) {
       imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
   }
   final AlertDialog.Builder builder  = new AlertDialog.Builder(getActivity());
   builder.setTitle("Search Event Triggered" ) ;
   builder.setMessage("Search event has been added to data collector" ) ;
   builder.setPositiveButton("OK" ,  null );
   builder.show();
}
```


This event will show up in Analytics reporting as a `SearchPhrase` event containing the parameters above.

Next, open the **Products** tab, either by navigating back to **Home** or by tapping on the **Products** tab on the tab bar. This view shows three images and three buttons. By tapping on any one of the buttons, you will trigger another event, this time for a `ProductId` event. The relevant code (from the `ProductsFragment` class) is shown below:

```java
@Override
public void onClick(View v) {
    final WTDataCollector _dc = ((WTBankApplication)getActivity().getApplication())._manager.getCollector();
    String cardString = "";
    String cardSku = "";
    HashMap<String, String> customParam = new HashMap();
    customParam.put("myCustomParam","myCustomValue");
    _dc.onAdClickEvent(null,"Sign-in button tapped!","Tap Event",null,"loginBtn");
    switch (v.getId()){
        case R.id.blue_credit_card:
            dialogShow("Blue Event Triggered","Product event has been added to data collector");
            cardString = "Blue";
            cardSku = "111";
            _dc.onProductView("/bank/products","user tapped product","tap",customParam,"Logged in",cardString,cardSku);
            break;
        case R.id.gold_credit_card:
            dialogShow("Gold Event Triggered","Product event has been added to data collector");
            cardString = "Gold";
            cardSku = "222";
            _dc.onProductView("/bank/products","user tapped product","tap",customParam,"Logged in",cardString,cardSku);
            break;
        case R.id.platinum_credit_card:
            dialogShow("Platinum Event Triggered","Product event has been added to data collector");
            cardString = "Platinum";
            cardSku = "333";
            _dc.onProductView("/bank/products","user tapped product","tap",customParam,"Logged in",cardString,cardSku);
            break;
    }
}
```

![sampleAppProducts](https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppProducts.png){height="542px" width=""}



The **Invest** tab item takes you to a page containing a WebView that pulls content from a web site. This is a demonstration of the hybrid (mixing native Android and web browser) functionality of the SDK. Session information that is started in the native app can be continued onto an Analytics-equipped web page and vice versa. This makes reporting on an end user very easy, since the session remains consistent from app to web.

The **Transactions -> Visit Our Web Site** link also demonstrates hybrid functionality, but shows how a session can continue into your Android web browser session and vice versa.

<p float="left"> 
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppInvest.png"  width="300px"> &nbsp &nbsp &nbsp &nbsp &nbsp
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppInvest2.png" width="305px">
</p>    



Next, the **Settings** tab takes you to a page that controls a few aspects of the SDK itself. The buttons set configuration settings in the SDK that affect its behavior.

![sampleAppSettings](https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppSettings.png){height="" width="300px"}


Each of these buttons toggles a boolean setting on or off. The specific call to the SDK looks like this:

```java
@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.toggleDataCollectionButton:
            toggleSetting(WTConfigKeys.ENABLED);
            dialogShow("Toggled!", "Data Collection");
            break;
        case R.id.toggleDebugLoggingButton:
            toggleSetting(WTConfigKeys.DEBUG);
            dialogShow("Toggled!", "Debug Logging");
            break;
        case R.id.pauseEventSendingButton:
            _dc.pauseEventTransmission();
            dialogShow("Paused!", "Event Sending");
            break;
        case R.id.startEventSendingButton:
            _dc.resumeEventTransmission();
            dialogShow("Started!", "Event Sending");
            break;
    }
}
```

The `settingName` string is passed into WTDatafCollector.getgetConfigValue(settingName) method and returns current state of the configuration.

```java
private void toggleSetting(String settingName)
{
    boolean enabled = Boolean.valueOf(_dc.getConfigValue(settingName));
    String toggle = "true";
    if (enabled) {
        toggle = "false";
    }
    Log.d("SettingsFragment",settingName+" set to "+toggle);
    _dc.setConfigSetting(settingName,toggle,true);
}
```


## Next Steps

Now that you've seen some of the things that are possible with the Webtrends Android SDK for Analytics, feel free to poke around the source code to see how it all works. We encourage you to experiment with the code and try new things. The SDK is very powerful, but also very simple to use. Go have some fun with it. 



:::(Internal) (oooops I did all that, and it's for Optimize!)
I did some more of this page before I realized it's for Optimize and shouldn't be here...Here's what I did, just in case you actually do want it included for some reason:

Finally, the **About Us** page is designed to show off the features of using Optimize(tm) in your application. The images below show the page rendered after applying several Optimize(tm) tests to the UI elements on the page (left) and the design view in the `res/layout/page_about.xml`, which shows the default UI elements (right). As you can see, the background color has been changed as well as the text fields and buttons. This change is done live when the SDK initializes and is rendered when the user navigates to this page.

Each of these UI elements are Webtrends Optimize SDK-specific subclasses. For example, the text field that contains "Old Text Field Contents" is a `com.webtrends.mobile.analytics.WTOptEditText` class which is a subclass of `EditText` enabling target of the Optimize test.

<p float="left"> 
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppAboutUs1.png"  width="300px"> &nbsp &nbsp &nbsp &nbsp &nbsp
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/SampleAppAboutUsDesignView.png" height="540px">
</p>    

Clicking on the Greet button at the top demonstrates the use of a live in-the-moment (non-cached) Optimize test. Unlike normal cached tests, the content of the test is not downloaded ahead of time. When the user clicks one of the greetings, the SDK calls home to the Optimize(tm) server, sending along some data, downloads content returned to it based on that data, and displays the results. In the case of the greeting, the app sends `greeting=personal or greeting=generic`. The Optimize server has a rule that responds to the value of **greeting** and returns some customized text.

<p float="left"> 
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppGreet.png" width="300px"> &nbsp &nbsp &nbsp &nbsp &nbsp
    <img src="https://cdn.document360.io/0afe9842-6601-4812-b9e2-2726979f2233/Images/Documentation/sampleAppGreetPersonal.png" width="300px">
</p>    

:::
