2015年6月13日 星期六

Android 開發(一百零一) google play Service Invite friend Api

google play service 在新版本推出了invite friend api ,讓你可以邀請google 上的朋友,
做法其實很簡單,

        Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
                .setMessage(getString(R.string.invitation_message))
                .setDeepLink(Uri.parse("http://tw.91mai.com/ref/0"))
                .build();
        startActivityForResult(intent, REQUEST_INVITE);

使用上面的方式將intent丟出,就會開啟邀請好友的選單,在邀請完成後利用onActivityResult去取得邀請的ids

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);

        if (requestCode == REQUEST_INVITE) {
            if (resultCode == RESULT_OK) {
                // Check how many invitations were sent and show message to the user
                // The ids array contains the unique invitation ids for each invitation sent
                // (one for each contact select by the user). You can use these for analytics
                // as the ID will be consistent on the sending and receiving devices.
                String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
                showMessage(getString(R.string.sent_invitations_fmt, ids.length));
            } else {
                // Sending failed or it was canceled, show failure message to the user
                showMessage(getString(R.string.send_failed));
            }
        }
    }

當你將邀請送出之後,你的朋友就會收到邀請下載的信,

out16.gif
out16.gif

如上方兩張圖,左邊是送出邀請的功能
右邊是好有收到邀請時,安裝後開啟時我們可以從intent中取得相關的link and id

為了要能夠在安裝時取得google play store送給我們的廣播,我們必須註冊receiver

    <receiver
           android:name="com.google.android.gms.samples.appinvite.ReferrerReceiver"
            android:exported="true"
            tools:ignore="ExportedReceiver">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

接著我們可以利用下方的程式碼取得相關的資訊

if (!AppInviteReferral.hasReferral(intent)) {
            Log.e(TAG, "Error: Intent does not contain App Invite");
           
// Extract referral information from the
String invitationId = AppInviteReferral.getInvitationId(
String deepLink = AppInviteReferral.getDeepLink(intent);

如果我們不需要track 成效的話,邀請的功能在這邊已經結束了
接下來要講的就是,我們可以利用google play service新的api 來trace我們的成效

在使用朋友贈送的信件開啟我們的app之後,我們可以利用google play service來track成效,

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .enableAutoManage(this, 0, this)
                .addApi(AppInvite.API)
                .build();

利用AppInvite.API  在api connect成功之後


  String invitationId = AppInviteReferral.getInvitationId(intent);

        // Note: these  calls return PendingResult(s), so one could also wait to see
        // if this succeeds instead of using fire-and-forget, as is shown here
        if (AppInviteReferral.isOpenedFromPlayStore(intent)) {
            AppInvite.AppInviteApi.updateInvitationOnInstall(mGoogleApiClient, invitationId);
        }

        // If your invitation contains deep link information such as a coupon code, you may
        // want to wait to call `convertInvitation` until the time when the user actually
        // uses the deep link data, rather than immediately upon receipt
        AppInvite.AppInviteApi.convertInvitation(mGoogleApiClient, invitationId);

我們可以取得inviteationId ,然後利用 updateInvitationOnInstall 告訴google analytics這個user已經成功安裝.

假設這個邀請是希望新安裝的user可以使用優惠券,那我們可以利用
convertInvitation告訴google analytics有多少使用者安裝後並且成功使用優惠券
也就是可以取得轉換率(我是這樣理解的XD)



最後,由於我是直接拿google 的sample就直接附上相關的網址吧,
有興趣的人可以去那邊做更深入的了解
https://developers.google.com/app-invites/android/guides/app?configured=true

然後下面的網址是appinvite設定的小幫手,在裡面跟著步驟設定之後,你google developer console的設定也會設定完成,個人覺得這個新界面真的是好用很多,大家可以參考看看
https://developers.google.com/mobile/add?platform=android&cntapi=appinvites&cnturl=https:%2F%2Fdevelopers.google.com%2Fapp-invites%2Fandroid%2Fguides%2Fapp%3Fconfigured%3Dtrue%23add-config&cntlbl=Continue%20Adding%20App%20Invites



沒有留言:

張貼留言