Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve push click behavior #247

Merged
merged 16 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import io.customer.android.sample.java_layout.R;
import io.customer.android.sample.java_layout.databinding.ActivitySimpleFragmentBinding;
import io.customer.android.sample.java_layout.ui.core.BaseActivity;
import io.customer.android.sample.java_layout.ui.dashboard.DashboardActivity;
Expand All @@ -16,8 +18,9 @@
import io.customer.android.sample.java_layout.ui.tracking.CustomEventTrackingFragment;
import io.customer.android.sample.java_layout.ui.user.AuthViewModel;
import io.customer.android.sample.java_layout.utils.ViewUtils;
import io.customer.sdk.tracking.TrackableScreen;

public class SimpleFragmentActivity extends BaseActivity<ActivitySimpleFragmentBinding> {
public class SimpleFragmentActivity extends BaseActivity<ActivitySimpleFragmentBinding> implements TrackableScreen {

public static final String FRAGMENT_CUSTOM_TRACKING_EVENT = "FRAGMENT_CUSTOM_TRACKING_EVENT";
public static final String FRAGMENT_DEVICE_ATTRIBUTES = "FRAGMENT_DEVICE_ATTRIBUTES";
Expand All @@ -34,6 +37,24 @@ public static Bundle getExtras(String fragmentName) {
return extras;
}

@Nullable
@Override
public String getScreenName() {
switch (mFragmentName) {
case FRAGMENT_CUSTOM_TRACKING_EVENT:
return getString(R.string.screen_label_custom_event);

case FRAGMENT_DEVICE_ATTRIBUTES:
return getString(R.string.screen_label_device_attributes);

case FRAGMENT_PROFILE_ATTRIBUTES:
return getString(R.string.screen_label_profile_attributes);

default:
return null;
}
}

@Override
protected ActivitySimpleFragmentBinding inflateViewBinding() {
return ActivitySimpleFragmentBinding.inflate(getLayoutInflater());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationManagerCompat;
Expand All @@ -34,8 +35,9 @@
import io.customer.android.sample.java_layout.ui.user.AuthViewModel;
import io.customer.android.sample.java_layout.utils.Randoms;
import io.customer.android.sample.java_layout.utils.ViewUtils;
import io.customer.sdk.tracking.TrackableScreen;

public class DashboardActivity extends BaseActivity<ActivityDashboardBinding> {
public class DashboardActivity extends BaseActivity<ActivityDashboardBinding> implements TrackableScreen {

private AuthViewModel authViewModel;
private CustomerIORepository customerIORepository;
Expand All @@ -56,6 +58,12 @@ public class DashboardActivity extends BaseActivity<ActivityDashboardBinding> {
}
});

@Nullable
@Override
public String getScreenName() {
return getString(R.string.screen_label_dashboard);
}

@Override
protected ActivityDashboardBinding inflateViewBinding() {
return ActivityDashboardBinding.inflate(getLayoutInflater());
Expand Down
4 changes: 4 additions & 0 deletions samples/java_layout/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@
<string name="error_text_input_field_empty">This field cannot be empty</string>
<string name="error_number_input_field_small">The value must be greater than or equal to %1$s</string>
<string name="token_copied">Token Copied!</string>
<string name="screen_label_dashboard">Dashboard</string>
<string name="screen_label_custom_event">Custom Event</string>
<string name="screen_label_device_attributes">Custom Device Attribute</string>
<string name="screen_label_profile_attributes">Custom Profile Attribute</string>
</resources>
4 changes: 4 additions & 0 deletions sdk/api/sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,7 @@ public abstract interface class io/customer/sdk/repository/preference/SitePrefer
public abstract fun setHttpRequestsPauseEnds (Ljava/util/Date;)V
}

public abstract interface class io/customer/sdk/tracking/TrackableScreen {
public abstract fun getScreenName ()Ljava/lang/String;
}

29 changes: 19 additions & 10 deletions sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.customer.sdk.repository.TrackRepository
import io.customer.sdk.repository.preference.CustomerIOStoredValues
import io.customer.sdk.repository.preference.doesExist
import io.customer.sdk.tracking.TrackableScreen
import io.customer.sdk.util.CioLogLevel
import io.customer.sdk.util.Seconds
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -545,17 +546,25 @@

private fun recordScreenViews(activity: Activity, attributes: CustomAttributes) {
val packageManager = activity.packageManager
return try {
val info = packageManager.getActivityInfo(
activity.componentName,
PackageManager.GET_META_DATA
)
val activityLabel = info.loadLabel(packageManager)

val screenName = activityLabel.toString().ifEmpty {
activity::class.java.simpleName.getScreenNameFromActivity()
try {

Check warning on line 549 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L549

Added line #L549 was not covered by tests
val screenName: String? = if (activity is TrackableScreen) {
// TrackableScreen takes precedence over manifest label
activity.getScreenName()

Check warning on line 552 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L552

Added line #L552 was not covered by tests
} else {
val info = packageManager.getActivityInfo(
mrehan27 marked this conversation as resolved.
Show resolved Hide resolved
activity.componentName,
PackageManager.GET_META_DATA

Check warning on line 556 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L554-L556

Added lines #L554 - L556 were not covered by tests
)
val activityLabel = info.loadLabel(packageManager)

Check warning on line 558 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L558

Added line #L558 was not covered by tests

activityLabel.toString().ifEmpty {
activity::class.java.simpleName.getScreenNameFromActivity()

Check warning on line 561 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L561

Added line #L561 was not covered by tests
}
}
// If screen name is null, we do not track the screen
screenName?.let {
screen(screenName, attributes)

Check warning on line 566 in sdk/src/main/java/io/customer/sdk/CustomerIO.kt

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/customer/sdk/CustomerIO.kt#L566

Added line #L566 was not covered by tests
}
screen(screenName, attributes)
} catch (e: PackageManager.NameNotFoundException) {
// if `PackageManager.NameNotFoundException` is thrown, is that a bug in the SDK or a problem with the customer's app?
// We may want to decide to log this as an SDK error, log it so customer notices it to fix it themselves, or we do nothing because this exception might not be a big issue.
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/main/java/io/customer/sdk/tracking/TrackableScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.customer.sdk.tracking

/**
* Optional interface for activities that should be tracked using automated screen tracking.
*/
interface TrackableScreen {
/**
* Retrieve the name that should be used for tracking the screen. This name
* should be unique for each screen.
*
* @return name for tracking the screen, or null if the screen shouldn't be tracked.
*/
fun getScreenName(): String?
}