-
Notifications
You must be signed in to change notification settings - Fork 625
Add getUtmParameters api method to PendingDynamicLinkData #2445
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...c-links/src/main/java/com/google/firebase/dynamiclinks/internal/DynamicLinkUTMParams.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.dynamiclinks.internal; | ||
|
||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
|
||
/** | ||
* Class to extract UTM parameters from firebase dynamic link. | ||
* | ||
* @hide | ||
*/ | ||
public class DynamicLinkUTMParams { | ||
|
||
@VisibleForTesting public static final String KEY_CAMPAIGN_BUNDLE = "_cmp"; | ||
@VisibleForTesting public static final String KEY_SCION_DATA_BUNDLE = "scionData"; | ||
@VisibleForTesting public static final String KEY_MEDIUM = "medium"; | ||
@VisibleForTesting public static final String KEY_SOURCE = "source"; | ||
@VisibleForTesting public static final String KEY_CAMPAIGN = "campaign"; | ||
|
||
/** Key to retrieve utm_medium from utm params bundle returned by {@link #asBundle()} */ | ||
public static final String KEY_UTM_MEDIUM = "utm_medium"; | ||
/** Key to retrieve utm_source from utm params bundle returned by {@link #asBundle()} */ | ||
public static final String KEY_UTM_SOURCE = "utm_source"; | ||
/** Key to retrieve utm_campaign from utm params bundle returned by {@link #asBundle()} */ | ||
public static final String KEY_UTM_CAMPAIGN = "utm_campaign"; | ||
|
||
private final DynamicLinkData dynamicLinkData; | ||
@NonNull private final Bundle utmParamsBundle; | ||
|
||
public DynamicLinkUTMParams(DynamicLinkData dynamicLinkData) { | ||
this.dynamicLinkData = dynamicLinkData; | ||
this.utmParamsBundle = initUTMParamsBundle(); | ||
} | ||
|
||
@NonNull | ||
public Bundle asBundle() { | ||
return new Bundle(utmParamsBundle); | ||
} | ||
|
||
private Bundle initUTMParamsBundle() { | ||
if (dynamicLinkData == null || dynamicLinkData.getExtensionBundle() == null) { | ||
return Bundle.EMPTY; | ||
} | ||
|
||
Bundle scionBundle = dynamicLinkData.getExtensionBundle().getBundle(KEY_SCION_DATA_BUNDLE); | ||
|
||
if (scionBundle == null) { | ||
return Bundle.EMPTY; | ||
} | ||
|
||
Bundle campaignBundle = scionBundle.getBundle(KEY_CAMPAIGN_BUNDLE); | ||
if (campaignBundle == null) { | ||
return Bundle.EMPTY; | ||
} | ||
|
||
Bundle bundle = new Bundle(); | ||
checkAndAdd(KEY_MEDIUM, KEY_UTM_MEDIUM, campaignBundle, bundle); | ||
checkAndAdd(KEY_SOURCE, KEY_UTM_SOURCE, campaignBundle, bundle); | ||
checkAndAdd(KEY_CAMPAIGN, KEY_UTM_CAMPAIGN, campaignBundle, bundle); | ||
return bundle; | ||
} | ||
|
||
/* | ||
* Checks and adds the value from source bundle to the destination bundle based on the source | ||
* key and destination key. | ||
*/ | ||
private void checkAndAdd( | ||
@NonNull String sourceKey, | ||
@NonNull String destKey, | ||
@NonNull Bundle source, | ||
@NonNull Bundle dest) { | ||
String value = source.getString(sourceKey); | ||
if (!TextUtils.isEmpty(value)) { | ||
dest.putString(destKey, value); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...nks/src/test/java/com/google/firebase/dynamiclinks/internal/DynamicLinkUTMParamsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.dynamiclinks.internal; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.assertNotNull; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
import android.os.Bundle; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class DynamicLinkUTMParamsTest { | ||
|
||
@Test | ||
public void testAsBundle_WithNullExtensions() { | ||
DynamicLinkUTMParams dynamicLinkUTMParams = new DynamicLinkUTMParams(getDynamicLinkData(null)); | ||
assertNonNullEmptyBundle(dynamicLinkUTMParams.asBundle()); | ||
} | ||
|
||
@Test | ||
public void testAsBundle_WithExtensionsButNullScionBundle() { | ||
Bundle extensions = getExtensions(); | ||
extensions.remove(DynamicLinkUTMParams.KEY_SCION_DATA_BUNDLE); | ||
DynamicLinkUTMParams dynamicLinkUTMParams = | ||
new DynamicLinkUTMParams(getDynamicLinkData(extensions)); | ||
assertNonNullEmptyBundle(dynamicLinkUTMParams.asBundle()); | ||
} | ||
|
||
@Test | ||
public void testAsBundle_WithExtensionsButNullCampaignBundle() { | ||
Bundle extensions = getExtensions(); | ||
extensions | ||
.getBundle(DynamicLinkUTMParams.KEY_SCION_DATA_BUNDLE) | ||
.remove(DynamicLinkUTMParams.KEY_CAMPAIGN_BUNDLE); | ||
DynamicLinkUTMParams dynamicLinkUTMParams = | ||
new DynamicLinkUTMParams(getDynamicLinkData(extensions)); | ||
assertNonNullEmptyBundle(dynamicLinkUTMParams.asBundle()); | ||
} | ||
|
||
@Test | ||
public void testAsBundle_WithExtensionsButNullUtmParams() { | ||
DynamicLinkUTMParams dynamicLinkUTMParams = | ||
new DynamicLinkUTMParams(getDynamicLinkData(getExtensions())); | ||
assertNonNullEmptyBundle(dynamicLinkUTMParams.asBundle()); | ||
} | ||
|
||
@Test | ||
public void testAsBundle_WithExtensionsButEmptyUtmParams() { | ||
DynamicLinkUTMParams dynamicLinkUTMParams = | ||
new DynamicLinkUTMParams(getDynamicLinkData(getExtensions("", "", ""))); | ||
assertNonNullEmptyBundle(dynamicLinkUTMParams.asBundle()); | ||
} | ||
|
||
@Test | ||
public void testAsBundle_WithExtensionsContainingUtmParams() { | ||
DynamicLinkUTMParams dynamicLinkUTMParams = | ||
new DynamicLinkUTMParams(getDynamicLinkData(getExtensions("m", "s", "c"))); | ||
// Non empty check | ||
assertFalse(isEmptyBundle(dynamicLinkUTMParams.asBundle())); | ||
|
||
// Comparing Utm params | ||
assertEquals( | ||
dynamicLinkUTMParams.asBundle().getString(DynamicLinkUTMParams.KEY_UTM_MEDIUM), "m"); | ||
assertEquals( | ||
dynamicLinkUTMParams.asBundle().getString(DynamicLinkUTMParams.KEY_UTM_SOURCE), "s"); | ||
assertEquals( | ||
dynamicLinkUTMParams.asBundle().getString(DynamicLinkUTMParams.KEY_UTM_CAMPAIGN), "c"); | ||
} | ||
|
||
private void assertNonNullEmptyBundle(Bundle bundle) { | ||
assertNotNull(bundle); | ||
assertTrue(isEmptyBundle(bundle)); | ||
} | ||
|
||
private boolean isEmptyBundle(Bundle bundle) { | ||
return bundle.size() == 0; | ||
} | ||
|
||
private Bundle getExtensions() { | ||
return getExtensions(null, null, null); | ||
} | ||
|
||
private Bundle getExtensions(String medium, String source, String campaign) { | ||
Bundle bundle = new Bundle(); | ||
Bundle scionBundle = new Bundle(); | ||
Bundle campaignBundle = new Bundle(); | ||
|
||
bundle.putBundle(DynamicLinkUTMParams.KEY_SCION_DATA_BUNDLE, scionBundle); | ||
scionBundle.putBundle(DynamicLinkUTMParams.KEY_CAMPAIGN_BUNDLE, campaignBundle); | ||
|
||
campaignBundle.putString(DynamicLinkUTMParams.KEY_MEDIUM, medium); | ||
campaignBundle.putString(DynamicLinkUTMParams.KEY_SOURCE, source); | ||
campaignBundle.putString(DynamicLinkUTMParams.KEY_CAMPAIGN, campaign); | ||
|
||
return bundle; | ||
} | ||
|
||
private DynamicLinkData getDynamicLinkData(Bundle extensions) { | ||
return new DynamicLinkData(null, null, 0, 0L, extensions, null); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.