本開發人員指南說明如何使用 Android 版 Google Analytics (分析) SDK v4,評估社交互動。
總覽
社交互動評估可讓您評估使用者與各種社交網路分享和嵌入內容推薦小工具的互動。
社交互動包含下列欄位:
欄位名稱 |
類型 |
必要 |
說明 |
社群網路 |
String |
是 |
與使用者互動的社群網路 (例如 Facebook、Google+、Twitter 等)。 |
社群媒體活動 |
String |
是 |
所執行的社交動作 (例如按讚、分享、+1 等)。 |
社交目標 |
String |
否 |
發生社交動作的內容 (例如特定文章或影片)。 |
Google Analytics (分析) SDK v4 Android 版收集的社交互動資料可透過自訂報表和 Core Reporting API。
導入作業
如要將社交互動傳送至 Google Analytics (分析),請使用 SocialBuilder
,如以下範例所示:
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send social interaction.
t.send(new HitBuilders.SocialBuilder()
.setNetwork(getSocialNetwork())
.setAction(getSocialAction())
.setTarget(getSocialTarget())
.build());
如要進一步瞭解 getTracker
方法,請參閱「
進階設定」。
查看範例應用程式中的程式碼片段
以下程式碼片段是傳送社交互動的 Fragment
範例:
package com.google.android.apps.mobileplayground;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.apps.mobileplayground.AnalyticsSampleApp.TrackerName;
import com.google.android.apps.mobileplayground.MobilePlayground.UserInputException;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
/**
* Class to exercise Social hits.
*/
public class SocialFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.social, container, false);
final Button sendButton = (Button) view.findViewById(R.id.socialSend);
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send social interaction.
t.send(new HitBuilders.SocialBuilder()
.setNetwork(getSocialNetwork())
.setAction(getSocialAction())
.setTarget(getSocialTarget())
.build());
} catch (UserInputException e) {
e.printStackTrace();
}
}
});
final Button dispatchButton = (Button) view.findViewById(R.id.socialDispatch);
dispatchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Manually start a dispatch (Unnecessary if the tracker has a dispatch interval)
GoogleAnalytics.getInstance(getActivity().getBaseContext()).dispatchLocalHits();
}
});
return view;
}
private String getSocialNetwork() throws UserInputException {
String result =
((EditText) getView().findViewById(R.id.editSocialNetwork)).getText().toString().trim();
if (result.length() == 0) {
throw new UserInputException(getString(R.string.socialNetworkWarning));
}
return result;
}
private String getSocialAction() throws UserInputException {
String result =
((EditText) getView().findViewById(R.id.editSocialAction)).getText().toString().trim();
if (result.length() == 0) {
throw new UserInputException(getString(R.string.socialActionWarning));
}
return result;
}
private String getSocialTarget() {
return ((EditText) getView().findViewById(R.id.editSocialTarget)).getText().toString().trim();
}
}