Rich-text Reply

Android - Set up View Goal on Fragment using Editor

Optimizeler 04-26-16

Android - Set up View Goal on Fragment using Editor

Is there any way to set up a View goal using the Optimizely Live Editor on a single Fragment? It's only detecting Activities and that's not what I am looking for.

tedroddy 04-27-16
 

Re: Android - Set up View Goal on Fragment using Editor

Optimizeler,

 

Typically, we recommend using custom events in order to track view goals if they’re not being tracked as expected.  However, you may want to still be able to track view goals.

 

Recommended implementation (Option 1):

You can use Optimizely.trackViewGoal("frag1" + id) in each of your fragments in the onResume method.  This method is useful if you don’t want to extend your base class (because you might be already extending another library’s base fragment).  

 

Alternative implementation (Option 2):

If you aren’t extending another base class, you can extend the OptimizelyFragment class.

  1. In order for view goals to be detected in their app when using fragments, a customer needs to either extend OptimizelyFragment (or OptimizelyListFragment) in each of their Fragment classes or their base Fragment class.
  2. By default we use the name of the fragment class as the view id.  If a customer wants to override this, which they would want to do if you use the fragment in multiple places in an activity you would do this (You just override `getViewId` and provide a unique id.):
package com.optimzelydev.optimizelydevelopmentapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.optimizely.fragment.OptimizelySupportFragment;
import com.optimzelydev.optimizelydevelopmentapp.dummy.DummyContent;

/**
* A fragment representing a single Item detail screen.
* This fragment is either contained in a {@link ItemListActivity}
* in two-pane mode (on tablets) or a {@link ItemDetailActivity}
* on handsets.
*/
public class ItemDetailFragment extends OptimizelySupportFragment {
   /**
    * The fragment argument representing the item ID that this fragment
    * represents.
    */
   public static final String ARG_ITEM_ID = "item_id";

   /**
    * The dummy content this fragment is presenting.
    */
   private DummyContent.DummyItem mItem;

   /**
    * Mandatory empty constructor for the fragment manager to instantiate the
    * fragment (e.g. upon screen orientation changes).
    */
   public ItemDetailFragment() {
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       if (getArguments().containsKey(ARG_ITEM_ID)) {
           // Load the dummy content specified by the fragment
           // arguments. In a real-world scenario, use a Loader
           // to load content from a content provider.
           mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
       }
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

       // Show the dummy content as text in a TextView.
       if (mItem != null) {
           ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
       }

       return rootView;
   }

   @Override
   public String getViewId() {
       return "itemDetailFrag" + mItem.id; // mItem.id makes the getViewId unique
   }
}

 

Let me know if this helps or if you have any other questions!

 

Best,
Ted

 

Best,
Ted
Optimizely