Android Open Source - GADemoGTM Main Activity






From Project

Back to project page GADemoGTM.

License

The source code is released under:

GNU General Public License

If you think the Android project GADemoGTM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.pdro.gademogtm;
//from  w w  w. ja  v  a  2  s . c  o  m
import com.pdro.gademogtm.gtm.Utils;
import com.pdro.gademogtm.gtm.ContainerHolderSingleton;
import com.pdro.gademogtm.tools.ColorWheel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Loads the main page with buttons for:
 *   - manual dispatch
 *   - container refresh
 *   - exception generation
 *   - event tracking (captures value of the custom dimension in the text field)
 *   - new screen (launches the {@link MessageActivity})
 */
// todo add better comments
public class MainActivity extends Activity {

    private ColorWheel mColorWheel = new ColorWheel();
    private String screenName = "Main Screen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // variables dealing with layout
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mainScreenLayout);
        final Button manualDispatchButton =  (Button) findViewById(R.id.manualDispatchButton);
        final Button refreshContainerButton = (Button) findViewById(R.id.containerRefreshButton);
        final TextView cdValueField = (TextView) findViewById(R.id.customDimensionValue);
        final Button exceptionButton = (Button) findViewById(R.id.generateExceptionButton);
        final Button eventButton = (Button) findViewById(R.id.trackEventButton);
        final Button nextScreenButton = (Button) findViewById(R.id.launchSecondScreenButton);

        // Create a GA Screen View
        mainScreenView(screenName);

        // Button Listener for MANUAL DISPATCH
        manualDispatchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                String category = "Admin";
                String action = "Dispatch";
                String label = getLastRefreshTimeString();
                String value = mainGetMacroValue("gaDispatchPeriod");

                // Create a GA Event
                mainCreateEvent(screenName, category, action, label, value);

                // Dispatch hits
                mainDispatchHits();

                Toast.makeText(getApplicationContext(), "Hits Dispatched!",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Button Listener for CONTAINER REFRESH
        refreshContainerButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String category = "Admin";
                String action = "Refresh";
                String label = refreshContainerButton.getText().toString();
                String value = mColorWheel.getCurrentColor();

                // Create a GA Event
                mainCreateEvent(screenName, category, action, label, value);

                // Refresh Container
                mainRefreshContainer();

                Toast.makeText(getApplicationContext(), "Container Refreshed!",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Button Listener for EXCEPTION
        exceptionButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mainGenerateException(screenName);
                Toast.makeText(getApplicationContext(), "Exception sent to GA",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Button Listener for EVENT
        eventButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                eventButton.setTextColor(color);
                nextScreenButton.setTextColor(color);
                refreshContainerButton.setTextColor(color);

                String cdValue = cdValueField.getText().toString();
                String category = "Interaction";
                String action = "Button Press";
                String label = eventButton.getText().toString() + " CD value: " + cdValue;
                String value = "";

                // Create a GA Event
                mainCreateEvent(screenName, category, action, label, value);

                Toast.makeText(getApplicationContext(), "Event sent to GA",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Button Listener for SECOND SCREEN
        nextScreenButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                startNewActivity();
            }
        });
    }

    // todo add section javadoc comments
    // todo parametrize the Message Activity.class argument
    // todo add method javadoc comments
    private void startNewActivity() {
        Intent intent = new Intent(MainActivity.this, MessageActivity.class);
        startActivity(intent);
    }

    // todo replace "GADemoGTM" string with app name parameter
    // todo add method javadoc comments
    public void mainScreenView(String screenName) {
        Utils.pushOpenScreenEvent(this, screenName);
        Log.e("GADemoGTM", "Screen: " + screenName);
    }

    // todo replace "GADemoGTM" string with app name parameter
    // todo add method javadoc comments
    public void mainDispatchHits() {
        Utils.dispatchHits(this);
        Log.e("GADemoGTM", "Dispatching hits!");
    }

    // todo add method javadoc comments
    public void mainRefreshContainer() {
        ContainerHolderSingleton.refreshContainer();
    }

    // todo add method javadoc comments
    public String mainGetMacroValue(String macroName) {
        return ContainerHolderSingleton.getContainerHolder().getContainer().getString(macroName);
    }

    // todo add method javadoc comments
    public String getLastRefreshTimeString() {
        String containerId = ContainerHolderSingleton.getContainerHolder().getContainer().getContainerId();
        Long refreshTime = ContainerHolderSingleton.getContainerHolder().getContainer().getLastRefreshTime();

        return containerId + " - last refreshed at: " + refreshTime.toString();
    }

    // todo replace "GADemoGTM" string with app name parameter
    // todo add method javadoc comments
    public void mainGenerateException(String screenName) {
        Utils.pushException(this, screenName);
        Log.e("GADemoGTM", screenName);
    }

    // todo replace "GADemoGTM" string with app name parameter
    // todo add method javadoc comments
    public void mainCreateEvent(String screenName, String category, String action, String label, String value) {
        Utils.pushEvent(this, screenName, category, action, label, value);
        Log.e("GADemoGTM Event", "Screen: " + screenName +
                ", Category: " + category +
                ", Action: " + action +
                ", Label: " + label +
                ", Value: " + value);
    }

}




Java Source Code List

com.pdro.gademogtm.MainActivity.java
com.pdro.gademogtm.MessageActivity.java
com.pdro.gademogtm.SplashScreen.java
com.pdro.gademogtm.gtm.ContainerHolderSingleton.java
com.pdro.gademogtm.gtm.Utils.java
com.pdro.gademogtm.tools.ColorWheel.java
com.pdro.gademogtm.tools.FactBook.java