Google Play安装引荐来源网址是大多数应用程序中使用的API,但在应用程序中看不到。此功能在后台运行,用于检查从应用程序获取最多下载资源的来源。 Google Play Install Referrer API会告诉我们该应用程序必须从何处安装和源。这将有助于我们改善应用程序在不同平台上的存在性。
Google Play Referrer API的用途是什么?
Google Play Referrer API为我们提供了应用安装位置的信息,无论它是Play商店还是任何其他平台。借助此API,我们可以跟踪用户下载我们的应用程序所采取的操作。以下是一些我们可以使用此API收集的重要数据。
- 借助此API,我们可以跟踪用户从何处安装了我们的应用程序。我们可以获得从中下载我们的应用程序的URL。
- 当用户单击引荐来源网址时,我们可以获得时间戳记。
- 当用户从特定的URL下载我们的应用程序时,我们可以获得该用户的时间戳。
- 首次安装我们的应用程序时,我们可以获得该应用程序的版本。
- 我们可以跟踪用户在过去7天中是否使用过该应用的即时体验。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在其中添加Google Play Referrer API,并在简单的文本视图中显示此API跟踪的数据。请注意,由于我们尚未将我们的应用发布到Google Play。因此,我们只会获取以自然方式安装应用程序的来源。下面是屏幕快照,在其中我们将看到我们将要构建的内容。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:在build.gradle文件中添加Google Play Referrer API的依赖项
导航至Gradle脚本,然后至build.gradle(Module)级别。在依赖性部分的build.gradle文件中添加以下行。
// below is the dependency for referer
implementation “com.android.installreferrer:installreferrer:2.2”
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
public class MainActivity extends AppCompatActivity {
// creating variables for text view.
private TextView refrerTV;
// variable for install referer client.
InstallReferrerClient referrerClient;
// creating an empty string for our referer.
String refrer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing all our variables.
refrerTV = findViewById(R.id.idTVRefrer);
// on below line we are building our install referrer client and building it.
referrerClient = InstallReferrerClient.newBuilder(this).build();
// on below line we are starting its connection.
referrerClient.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
// this method is called when install referer setup is finished.
switch (responseCode) {
// we are using switch case to check the response.
case InstallReferrerClient.InstallReferrerResponse.OK:
// this case is called when the status is OK and
ReferrerDetails response = null;
try {
// on below line we are getting referrer details
// by calling get install referrer.
response = referrerClient.getInstallReferrer();
// on below line we are getting referrer url.
String referrerUrl = response.getInstallReferrer();
// on below line we are getting referrer click time.
long referrerClickTime = response.getReferrerClickTimestampSeconds();
// on below line we are getting app install time
long appInstallTime = response.getInstallBeginTimestampSeconds();
// on below line we are getting our time when
// user has used our apps instant experience.
boolean instantExperienceLaunched = response.getGooglePlayInstantParam();
// on below line we are getting our
// apps install referrer.
refrer = response.getInstallReferrer();
// on below line we are setting all detail to our text view.
refrerTV.setText("Referrer is : \n" + referrerUrl + "\n" + "Referrer Click Time is : " + referrerClickTime + "\nApp Install Time : " + appInstallTime);
} catch (RemoteException e) {
// handling error case.
e.printStackTrace();
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
// API not available on the current Play Store app.
Toast.makeText(MainActivity.this, "Feature not supported..", Toast.LENGTH_SHORT).show();
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
// Connection couldn't be established.
Toast.makeText(MainActivity.this, "Fail to establish connection", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(MainActivity.this, "Service disconnected..", Toast.LENGTH_SHORT).show();
}
});
}
}
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
public class MainActivity extends AppCompatActivity {
// creating variables for text view.
private TextView refrerTV;
// variable for install referer client.
InstallReferrerClient referrerClient;
// creating an empty string for our referer.
String refrer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing all our variables.
refrerTV = findViewById(R.id.idTVRefrer);
// on below line we are building our install referrer client and building it.
referrerClient = InstallReferrerClient.newBuilder(this).build();
// on below line we are starting its connection.
referrerClient.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
// this method is called when install referer setup is finished.
switch (responseCode) {
// we are using switch case to check the response.
case InstallReferrerClient.InstallReferrerResponse.OK:
// this case is called when the status is OK and
ReferrerDetails response = null;
try {
// on below line we are getting referrer details
// by calling get install referrer.
response = referrerClient.getInstallReferrer();
// on below line we are getting referrer url.
String referrerUrl = response.getInstallReferrer();
// on below line we are getting referrer click time.
long referrerClickTime = response.getReferrerClickTimestampSeconds();
// on below line we are getting app install time
long appInstallTime = response.getInstallBeginTimestampSeconds();
// on below line we are getting our time when
// user has used our apps instant experience.
boolean instantExperienceLaunched = response.getGooglePlayInstantParam();
// on below line we are getting our
// apps install referrer.
refrer = response.getInstallReferrer();
// on below line we are setting all detail to our text view.
refrerTV.setText("Referrer is : \n" + referrerUrl + "\n" + "Referrer Click Time is : " + referrerClickTime + "\nApp Install Time : " + appInstallTime);
} catch (RemoteException e) {
// handling error case.
e.printStackTrace();
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
// API not available on the current Play Store app.
Toast.makeText(MainActivity.this, "Feature not supported..", Toast.LENGTH_SHORT).show();
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
// Connection couldn't be established.
Toast.makeText(MainActivity.this, "Fail to establish connection", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(MainActivity.this, "Service disconnected..", Toast.LENGTH_SHORT).show();
}
});
}
}
现在运行您的应用程序,并查看该应用程序的输出。
Note: As our app is not published on Google Play so we will only get the referrer details as organic and other details will get as zero.
输出: