Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

Commit

Permalink
Update deflection tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
austintaylor committed Jan 9, 2014
1 parent a616cd0 commit 855ee8a
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
} else if (position != 1) {
Suggestion suggestion = (Suggestion) getModelAdapter().getItem(position);
Session.getInstance().setSuggestion(suggestion);
SuggestionDialogFragment dialog = new SuggestionDialogFragment(suggestion);
SuggestionDialogFragment dialog = new SuggestionDialogFragment(suggestion, null);
dialog.show(getSupportFragmentManager(), "SuggestionDialogFragment");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.uservoice.uservoicesdk.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ViewGroup;

import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import com.uservoice.uservoicesdk.R;
import com.uservoice.uservoicesdk.flow.InitManager;
import com.uservoice.uservoicesdk.ui.InstantAnswersAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.util.Log;
import com.uservoice.uservoicesdk.Session;
import com.uservoice.uservoicesdk.model.BaseModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
import java.util.Date;
import java.util.Map;

import android.net.http.AndroidHttpClient;
import com.uservoice.uservoicesdk.UserVoice;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;

import com.uservoice.uservoicesdk.Session;
import com.uservoice.uservoicesdk.UserVoice;

public class BabayagaTask extends AsyncTask<String, String, Void> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.util.List;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.uservoice.uservoicesdk.Session;
import com.uservoice.uservoicesdk.babayaga.Babayaga;
import com.uservoice.uservoicesdk.model.Article;
import com.uservoice.uservoicesdk.model.BaseModel;
Expand All @@ -25,25 +25,41 @@ public class Deflection {
private static int interactionIdentifier = Integer.parseInt(String.valueOf(new Date().getTime()).substring(4));
private static String searchText;

public static void trackDeflection(String kind, BaseModel deflector) {
public static void trackDeflection(String kind, String deflectingType, BaseModel deflector) {
Map<String, String> params = deflectionParams();
params.put("kind", kind);
params.put("deflecting_type", deflectingType);
params.put("deflector_id", String.valueOf(deflector.getId()));
params.put("deflector_type", (deflector instanceof Article) ? "Faq" : "Suggestion");
new RestTask(RestMethod.GET, "/clients/omnibox/deflections/upsert.json", params, getCallback()).execute();
}

public static void trackSearchDeflection(List<BaseModel> results) {
public static void trackSearchDeflection(List<BaseModel> results, String deflectingType) {
Map<String, String> params = deflectionParams();
params.put("kind", "list");
List<BasicNameValuePair> list = RestTask.paramsToList(params);
params.put("deflecting_type", deflectingType);
int articleResults = 0;
int suggestionResults = 0;
int index = 0;
for (BaseModel model : results) {
if (model instanceof Suggestion)
list.add(new BasicNameValuePair("suggestion_ids[]", String.valueOf(model.getId())));
else if (model instanceof Article)
list.add(new BasicNameValuePair("faq_ids[]", String.valueOf(model.getId())));
String prefix = "results[" + String.valueOf(index) + "]";
params.put(prefix + "[position]", String.valueOf(index++));
params.put(prefix + "[deflector_id]", String.valueOf(model.getId()));
if (model instanceof Suggestion) {
suggestionResults++;
Suggestion suggestion = (Suggestion) model;
params.put(prefix + "[weight]", String.valueOf(suggestion.getWeight()));
params.put(prefix + "[deflector_type]", "Suggestion");
} else if (model instanceof Article) {
articleResults++;
Article article = (Article) model;
params.put(prefix + "[weight]", String.valueOf(article.getWeight()));
params.put(prefix + "[deflector_type]", "Faq");
}
}
new RestTask(RestMethod.GET, "/clients/omnibox/deflections/list_view.json", list, getCallback()).execute();
params.put("faq_results", String.valueOf(articleResults));
params.put("suggestion_results", String.valueOf(suggestionResults));
new RestTask(RestMethod.GET, "/clients/omnibox/deflections/list_view.json", params, getCallback()).execute();
}

public static void setSearchText(String query) {
Expand All @@ -69,10 +85,13 @@ public void onComplete(JSONObject result) throws JSONException {

private static Map<String, String> deflectionParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("uvts", Babayaga.getUvts());
if (Babayaga.getUvts() != null) {
params.put("uvts", Babayaga.getUvts());
}
params.put("channel", "android");
params.put("search_term", searchText);
params.put("interaction_identifier", String.valueOf(interactionIdentifier));
params.put("subdomain_id", String.valueOf(Session.getInstance().getClientConfig().getSubdomainId()));
return params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public class ArticleDialogFragment extends DialogFragmentBugfixed {

private final Article article;
private WebView webView;
private String deflectingType;

public ArticleDialogFragment(Article article) {
public ArticleDialogFragment(Article article, String deflectingType) {
this.article = article;
this.deflectingType = deflectingType;
}

@Override
Expand All @@ -41,7 +43,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@Override
public void onClick(DialogInterface dialog, int which) {
if (getActivity() instanceof InstantAnswersActivity) {
Deflection.trackDeflection("unhelpful", article);
Deflection.trackDeflection("unhelpful", deflectingType, article);
InstantAnswersActivity activity = (InstantAnswersActivity) getActivity();
InstantAnswersAdapter adapter = (InstantAnswersAdapter) activity.getListAdapter();
adapter.notHelpful();
Expand All @@ -57,7 +59,7 @@ public void onClick(DialogInterface dialog, int which) {
public void onClick(DialogInterface dialog, int which) {
Babayaga.track(Babayaga.Event.VOTE_ARTICLE, article.getId());
if (getActivity() instanceof InstantAnswersActivity) {
Deflection.trackDeflection("helpful", article);
Deflection.trackDeflection("helpful", deflectingType, article);
HelpfulDialogFragment helpfulDialog = new HelpfulDialogFragment();
helpfulDialog.show(getActivity().getSupportFragmentManager(), "HelpfulDialogFragment");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public class SubscribeDialogFragment extends DialogFragmentBugfixed {

private final Suggestion suggestion;
private final SuggestionDialogFragment suggestionDialog;
private final String deflectingType;

public SubscribeDialogFragment(Suggestion suggestion, SuggestionDialogFragment suggestionDialog) {
public SubscribeDialogFragment(Suggestion suggestion, SuggestionDialogFragment suggestionDialog, String deflectingType) {
this.suggestion = suggestion;
this.suggestionDialog = suggestionDialog;
this.deflectingType = deflectingType;
}

@Override
Expand All @@ -52,12 +54,10 @@ public void run() {
@Override
public void onModel(Suggestion model) {
if (getActivity() instanceof InstantAnswersActivity)
Deflection.trackDeflection("subscribed", model);
Deflection.trackDeflection("subscribed", deflectingType, model);
suggestionDialog.suggestionSubscriptionUpdated(model);
dialog.dismiss();
}

;
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public class SuggestionDialogFragment extends DialogFragmentBugfixed {
private View headerView;
private View view;
private Context context;
private String deflectingType;

public SuggestionDialogFragment(Suggestion suggestion) {
public SuggestionDialogFragment(Suggestion suggestion, String deflectingType) {
this.suggestion = suggestion;
this.deflectingType = deflectingType;
}

@Override
Expand All @@ -58,7 +60,7 @@ public void onClick(View v) {
@Override
public void onModel(Suggestion model) {
if (getActivity() instanceof InstantAnswersActivity)
Deflection.trackDeflection("subscribed", model);
Deflection.trackDeflection("subscribed", deflectingType, model);
suggestionSubscriptionUpdated(model);
}
};
Expand All @@ -73,7 +75,7 @@ public void run() {
}
});
} else {
SubscribeDialogFragment dialog = new SubscribeDialogFragment(suggestion, SuggestionDialogFragment.this);
SubscribeDialogFragment dialog = new SubscribeDialogFragment(suggestion, SuggestionDialogFragment.this, deflectingType);
dialog.show(getFragmentManager(), "SubscribeDialogFragment");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Article extends BaseModel {
private String title;
private String html;
private String topicName;
private int weight;

public static void loadAll(final Callback<List<Article>> callback) {
Map<String, String> params = new HashMap<String, String>();
Expand Down Expand Up @@ -60,6 +61,9 @@ public void load(JSONObject object) throws JSONException {
super.load(object);
title = getString(object, "question");
html = getHtml(object, "answer_html");
if (object.has("normalized_weight")) {
weight = object.getInt("normalized_weight");
}
if (!object.isNull("topic")) {
JSONObject topic = object.getJSONObject("topic");
topicName = topic.getString("name");
Expand All @@ -77,4 +81,8 @@ public String getHtml() {
public String getTopicName() {
return topicName;
}

public int getWeight() {
return weight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.List;

import android.content.SharedPreferences;
import android.util.Log;
import com.uservoice.uservoicesdk.Session;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;

import com.uservoice.uservoicesdk.Session;
import com.uservoice.uservoicesdk.rest.Callback;
import com.uservoice.uservoicesdk.rest.RestTaskCallback;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.json.JSONObject;

import com.uservoice.uservoicesdk.babayaga.Babayaga;
import com.uservoice.uservoicesdk.deflection.Deflection;
import com.uservoice.uservoicesdk.rest.Callback;
import com.uservoice.uservoicesdk.rest.RestTask;
import com.uservoice.uservoicesdk.rest.RestTaskCallback;
Expand All @@ -32,6 +31,7 @@ public class Suggestion extends BaseModel {
private int forumId;
private boolean subscribed;
private String forumName;
private int weight;

public static void loadSuggestions(Forum forum, int page, final Callback<List<Suggestion>> callback) {
Map<String, String> params = new HashMap<String, String>();
Expand Down Expand Up @@ -127,6 +127,9 @@ public void load(JSONObject object) throws JSONException {
adminResponseUserName = getString(responseUser, "name");
adminResponseAvatarUrl = getString(responseUser, "avatar_url");
}
if (object.has("normalized_weight")) {
weight = object.getInt("normalized_weight");
}
}

public String getForumName() {
Expand Down Expand Up @@ -196,5 +199,9 @@ public int getNumberOfSubscribers() {
public void commentPosted(Comment comment) {
numberOfComments += 1;
}

public int getWeight() {
return weight;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import java.util.Locale;
import java.util.Map;

import android.content.Context;
import android.net.http.AndroidHttpClient;
import oauth.signpost.OAuthConsumer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
Expand All @@ -30,6 +27,7 @@
import org.json.JSONObject;

import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;

import com.uservoice.uservoicesdk.Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected enum State {
protected EditText emailField;
protected EditText nameField;
protected int continueButtonMessage;
protected String deflectingType;

public InstantAnswersAdapter(FragmentActivity context) {
this.context = context;
Expand Down Expand Up @@ -123,8 +124,8 @@ public boolean isEnabled(int position) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int type = getItemViewType(position);
if (type == INSTANT_ANSWER) {
Deflection.trackDeflection("show", (BaseModel) getItem(position));
Utils.showModel(context, (BaseModel) getItem(position));
Deflection.trackDeflection("show", deflectingType, (BaseModel) getItem(position));
Utils.showModel(context, (BaseModel) getItem(position), deflectingType);
}
}

Expand Down Expand Up @@ -274,7 +275,8 @@ protected void onButtonTapped() {
Article.loadInstantAnswers(query, new DefaultCallback<List<BaseModel>>(context) {
@Override
public void onModel(List<BaseModel> model) {
Deflection.trackSearchDeflection(model);
List<BaseModel> results = model.subList(0, Math.min(model.size(), 3));
Deflection.trackSearchDeflection(results, deflectingType);
instantAnswers = model;
if (instantAnswers.isEmpty())
state = State.DETAILS;
Expand Down
8 changes: 6 additions & 2 deletions UserVoiceSDK/src/com/uservoice/uservoicesdk/ui/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ public static void displayInstantAnswer(View view, BaseModel model) {
}

public static void showModel(FragmentActivity context, BaseModel model) {
showModel(context, model, null);
}

public static void showModel(FragmentActivity context, BaseModel model, String deflectingType) {
if (model instanceof Article) {
ArticleDialogFragment fragment = new ArticleDialogFragment((Article) model);
ArticleDialogFragment fragment = new ArticleDialogFragment((Article) model, deflectingType);
fragment.show(context.getSupportFragmentManager(), "ArticleDialogFragment");
} else if (model instanceof Suggestion) {
SuggestionDialogFragment fragment = new SuggestionDialogFragment((Suggestion) model);
SuggestionDialogFragment fragment = new SuggestionDialogFragment((Suggestion) model, deflectingType);
fragment.show(context.getSupportFragmentManager(), "SuggestionDialogFragment");
} else if (model instanceof Topic) {
Session.getInstance().setTopic((Topic) model);
Expand Down

0 comments on commit 855ee8a

Please sign in to comment.