Skip to content

Commit 1d50ae8

Browse files
authored
feat(all): Add Gen2 Config (#2771)
1 parent b90e443 commit 1d50ae8

File tree

64 files changed

+3631
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3631
-200
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ __pycache__/
8787
**/credentials.json
8888
**/google_client_creds.json
8989
**/amplifyconfiguration*.json
90+
**/amplify_outputs.json
9091

9192
# IDE files
9293
.idea/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.annotations
17+
18+
/**
19+
* APIs marked with this annotation are visible for usage from the Amplify Flutter library, and are not intended
20+
* for external use. They may change or be removed without warning.
21+
*
22+
* We strongly recommend to not use such API.
23+
*/
24+
@RequiresOptIn(
25+
level = RequiresOptIn.Level.ERROR,
26+
message = "This API is for Amplify Flutter and should not be used elsewhere. " +
27+
"It could be removed or changed without notice."
28+
)
29+
@Target(
30+
AnnotationTarget.CLASS,
31+
AnnotationTarget.TYPEALIAS,
32+
AnnotationTarget.FUNCTION,
33+
AnnotationTarget.PROPERTY,
34+
AnnotationTarget.FIELD,
35+
AnnotationTarget.CONSTRUCTOR
36+
)
37+
annotation class AmplifyFlutterApi

aws-analytics-pinpoint/api/aws-analytics-pinpoint.api

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
public final class com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin : com/amplifyframework/analytics/AnalyticsPlugin {
22
public fun <init> ()V
3+
public fun <init> (Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;)V
4+
public synthetic fun <init> (Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
35
public fun configure (Lorg/json/JSONObject;Landroid/content/Context;)V
46
public fun disable ()V
57
public fun enable ()V
@@ -15,6 +17,34 @@ public final class com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsP
1517
public fun unregisterGlobalProperties ([Ljava/lang/String;)V
1618
}
1719

20+
public final class com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options {
21+
public static final field Companion Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Companion;
22+
public static final fun builder ()Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Builder;
23+
public final fun component1 ()J
24+
public final fun component2 ()Z
25+
public final fun copy (JZ)Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;
26+
public static synthetic fun copy$default (Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;JZILjava/lang/Object;)Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;
27+
public fun equals (Ljava/lang/Object;)Z
28+
public final fun getAutoFlushEventsInterval ()J
29+
public final fun getTrackLifecycleEvents ()Z
30+
public fun hashCode ()I
31+
public fun toString ()Ljava/lang/String;
32+
}
33+
34+
public final class com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Builder {
35+
public final fun autoFlushEventsInterval (J)Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Builder;
36+
public final fun getAutoFlushEventsInterval ()J
37+
public final fun getTrackLifecycleEvents ()Z
38+
public final synthetic fun setAutoFlushEventsInterval (J)V
39+
public final synthetic fun setTrackLifecycleEvents (Z)V
40+
public final fun trackLifecycleEvents (Z)Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Builder;
41+
}
42+
43+
public final class com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Companion {
44+
public final fun builder ()Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options$Builder;
45+
public final synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Lcom/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin$Options;
46+
}
47+
1848
public final class com/amplifyframework/analytics/pinpoint/AnalyticsChannelEventName : java/lang/Enum {
1949
public static final field FLUSH_EVENTS Lcom/amplifyframework/analytics/pinpoint/AnalyticsChannelEventName;
2050
public static fun getEntries ()Lkotlin/enums/EnumEntries;

aws-analytics-pinpoint/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ dependencies {
4141
testImplementation(libs.test.androidx.junit)
4242
testImplementation(libs.test.androidx.core)
4343
testImplementation(libs.test.kotlin.coroutines)
44+
testImplementation(libs.test.kotest.assertions)
45+
testImplementation(project(":testutils"))
4446
testImplementation(project(":aws-analytics-pinpoint"))
4547

4648
androidTestImplementation(project(":testutils"))

aws-analytics-pinpoint/src/main/java/com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPlugin.kt

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ import com.amplifyframework.analytics.AnalyticsEventBehavior
2020
import com.amplifyframework.analytics.AnalyticsPlugin
2121
import com.amplifyframework.analytics.AnalyticsProperties
2222
import com.amplifyframework.analytics.UserProfile
23+
import com.amplifyframework.annotations.InternalAmplifyApi
2324
import com.amplifyframework.auth.CognitoCredentialsProvider
25+
import com.amplifyframework.core.configuration.AmplifyOutputsData
2426
import org.json.JSONObject
2527

2628
/**
2729
* The plugin implementation for Amazon Pinpoint in Analytics category.
2830
*/
29-
class AWSPinpointAnalyticsPlugin : AnalyticsPlugin<PinpointClient>() {
31+
class AWSPinpointAnalyticsPlugin @JvmOverloads constructor(
32+
private val userOptions: Options? = null
33+
) : AnalyticsPlugin<PinpointClient>() {
3034

3135
private val pluginKey = "awsPinpointAnalyticsPlugin"
3236
private val analyticsConfigKey = "pinpointAnalytics"
@@ -79,28 +83,47 @@ class AWSPinpointAnalyticsPlugin : AnalyticsPlugin<PinpointClient>() {
7983
configBuilder.withRegion(
8084
pinpointAnalyticsConfigJson.getString(PinpointConfigurationKey.REGION.configurationKey)
8185
)
82-
if (pinpointAnalyticsConfigJson.has(PinpointConfigurationKey.AUTO_FLUSH_INTERVAL.configurationKey)) {
83-
configBuilder.withAutoFlushEventsInterval(
84-
pinpointAnalyticsConfigJson.getLong(PinpointConfigurationKey.AUTO_FLUSH_INTERVAL.configurationKey)
85-
)
86-
}
87-
if (pinpointAnalyticsConfigJson.has(PinpointConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.configurationKey)) {
88-
configBuilder.withTrackAppLifecycleEvents(
89-
pinpointAnalyticsConfigJson.getBoolean(
90-
PinpointConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.configurationKey
86+
87+
// Use the programmatic options if they were supplied, otherwise read additional options from the
88+
// amplifyconfiguration file
89+
if (userOptions != null) {
90+
configBuilder.withAutoFlushEventsInterval(userOptions.autoFlushEventsInterval)
91+
.withTrackAppLifecycleEvents(userOptions.trackLifecycleEvents)
92+
} else {
93+
if (pinpointAnalyticsConfigJson.has(PinpointConfigurationKey.AUTO_FLUSH_INTERVAL.configurationKey)) {
94+
configBuilder.withAutoFlushEventsInterval(
95+
pinpointAnalyticsConfigJson.getLong(PinpointConfigurationKey.AUTO_FLUSH_INTERVAL.configurationKey)
9196
)
92-
)
97+
}
98+
if (pinpointAnalyticsConfigJson.has(PinpointConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.configurationKey)) {
99+
configBuilder.withTrackAppLifecycleEvents(
100+
pinpointAnalyticsConfigJson.getBoolean(
101+
PinpointConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.configurationKey
102+
)
103+
)
104+
}
93105
}
94106
val awsAnalyticsConfig = configBuilder.build()
107+
configure(awsAnalyticsConfig, context)
108+
}
109+
110+
@InternalAmplifyApi
111+
override fun configure(configuration: AmplifyOutputsData, context: Context) {
112+
val options = this.userOptions ?: Options.defaults()
113+
val analyticsConfig = AWSPinpointAnalyticsPluginConfiguration.from(configuration, options)
114+
configure(analyticsConfig, context)
115+
}
116+
117+
private fun configure(configuration: AWSPinpointAnalyticsPluginConfiguration, context: Context) {
95118
pinpointManager = PinpointManager(
96119
context,
97-
awsAnalyticsConfig,
120+
configuration,
98121
CognitoCredentialsProvider()
99122
)
100123

101124
awsPinpointAnalyticsPluginBehavior = AWSPinpointAnalyticsPluginBehavior(
102125
pinpointManager.analyticsClient,
103-
pinpointManager.targetingClient,
126+
pinpointManager.targetingClient
104127
)
105128
}
106129

@@ -111,6 +134,69 @@ class AWSPinpointAnalyticsPlugin : AnalyticsPlugin<PinpointClient>() {
111134
override fun getVersion(): String {
112135
return BuildConfig.VERSION_NAME
113136
}
137+
138+
/**
139+
* Options that can be specified to fine-tune the behavior of the Pinpoint Analytics Plugin.
140+
*/
141+
data class Options internal constructor(
142+
/**
143+
* The interval between sends of queued analytics events, in milliseconds
144+
*/
145+
val autoFlushEventsInterval: Long,
146+
147+
/**
148+
* If true then the plugin will stop sessions when the app goes to the background
149+
*/
150+
val trackLifecycleEvents: Boolean
151+
) {
152+
companion object {
153+
/**
154+
* Create a new [Builder] instance
155+
*/
156+
@JvmStatic
157+
fun builder() = Builder()
158+
159+
/**
160+
* Create an [AWSPinpointAnalyticsPlugin.Options] instance
161+
*/
162+
@JvmSynthetic
163+
operator fun invoke(func: Builder.() -> Unit) = Builder().apply(func).build()
164+
165+
internal fun defaults() = builder().build()
166+
}
167+
168+
/**
169+
* Builder API for constructing [AWSPinpointAnalyticsPlugin.Options] instances
170+
*/
171+
class Builder internal constructor() {
172+
/**
173+
* Set the interval between sends of queued analytics events, in milliseconds
174+
*/
175+
var autoFlushEventsInterval: Long = AWSPinpointAnalyticsPluginConfiguration.DEFAULT_AUTO_FLUSH_INTERVAL
176+
@JvmSynthetic set
177+
178+
/**
179+
* Set whether or not the plugin will stop/start sessions when the app goes to the background/foreground.
180+
*/
181+
var trackLifecycleEvents: Boolean = true
182+
@JvmSynthetic set
183+
184+
/**
185+
* Set the interval between sends of queed analytics events, in milliseconds
186+
*/
187+
fun autoFlushEventsInterval(value: Long) = apply { autoFlushEventsInterval = value }
188+
189+
/**
190+
* Set whether or not the plugin will stop/start sessions when the app goes to the background/foreground.
191+
*/
192+
fun trackLifecycleEvents(value: Boolean) = apply { trackLifecycleEvents = value }
193+
194+
internal fun build() = Options(
195+
autoFlushEventsInterval = autoFlushEventsInterval,
196+
trackLifecycleEvents = trackLifecycleEvents
197+
)
198+
}
199+
}
114200
}
115201

116202
private enum class PinpointConfigurationKey(
@@ -137,5 +223,5 @@ private enum class PinpointConfigurationKey(
137223
/**
138224
* Whether to track app lifecycle events automatically.
139225
*/
140-
TRACK_APP_LIFECYCLE_EVENTS("trackAppLifecycleEvents");
226+
TRACK_APP_LIFECYCLE_EVENTS("trackAppLifecycleEvents")
141227
}

aws-analytics-pinpoint/src/main/java/com/amplifyframework/analytics/pinpoint/AWSPinpointAnalyticsPluginConfiguration.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@
1515

1616
package com.amplifyframework.analytics.pinpoint;
1717

18+
import androidx.annotation.NonNull;
19+
20+
import com.amplifyframework.AmplifyException;
21+
import com.amplifyframework.analytics.AnalyticsException;
22+
import com.amplifyframework.annotations.InternalAmplifyApi;
23+
import com.amplifyframework.core.configuration.AmplifyOutputsData;
24+
1825
/**
1926
* Configuration options for Amplify Analytics Pinpoint plugin.
2027
*/
2128
final class AWSPinpointAnalyticsPluginConfiguration {
2229

23-
private static final long DEFAULT_AUTO_FLUSH_INTERVAL = 30000L;
30+
static final long DEFAULT_AUTO_FLUSH_INTERVAL = 30000L;
2431

2532
// Pinpoint plugin configuration options
2633
private final String appId;
27-
private final boolean trackAppLifecycleEvents;
2834
private final String region;
2935
private final long autoFlushEventsInterval;
36+
private final boolean trackAppLifecycleEvents;
3037

3138
private AWSPinpointAnalyticsPluginConfiguration(Builder builder) {
3239
this.appId = builder.appId;
3340
this.region = builder.region;
34-
this.trackAppLifecycleEvents = builder.trackAppLifecycleEvents;
3541
this.autoFlushEventsInterval = builder.autoFlushEventsInterval;
42+
this.trackAppLifecycleEvents = builder.trackAppLifecycleEvents;
3643
}
3744

3845
/**
@@ -73,20 +80,43 @@ boolean isTrackAppLifecycleEvents() {
7380
/**
7481
* Return a builder that can be used to construct a new instance of
7582
* {@link AWSPinpointAnalyticsPluginConfiguration}.
76-
* @return An {@link PinpointProperties.Builder} instance
83+
* @return An {@link AWSPinpointAnalyticsPluginConfiguration.Builder} instance
7784
*/
7885
static Builder builder() {
7986
return new Builder();
8087
}
8188

89+
@InternalAmplifyApi
90+
static AWSPinpointAnalyticsPluginConfiguration from(
91+
@NonNull AmplifyOutputsData outputs,
92+
@NonNull AWSPinpointAnalyticsPlugin.Options options
93+
) throws AmplifyException {
94+
AmplifyOutputsData.Analytics analytics = outputs.getAnalytics();
95+
if (analytics == null || analytics.getAmazonPinpoint() == null) {
96+
throw new AnalyticsException(
97+
"Missing Analytics configuration",
98+
"Ensure that analytics is enabled and exists in your configuration file"
99+
);
100+
}
101+
102+
// Note: autoFlushEventsInterval is not supported in Gen2 config.
103+
// Customers should use the programmatic plugin options API instead.
104+
return builder()
105+
.withAppId(analytics.getAmazonPinpoint().getAppId())
106+
.withRegion(analytics.getAmazonPinpoint().getAwsRegion())
107+
.withAutoFlushEventsInterval(options.getAutoFlushEventsInterval())
108+
.withTrackAppLifecycleEvents(options.getTrackLifecycleEvents())
109+
.build();
110+
}
111+
82112
/**
83113
* Used for fluent construction of an immutable {@link AWSPinpointAnalyticsPluginConfiguration} object.
84114
*/
85115
static final class Builder {
86116
private String appId;
87-
private boolean trackAppLifecycleEvents = false;
88117
private String region;
89118
private long autoFlushEventsInterval = DEFAULT_AUTO_FLUSH_INTERVAL;
119+
private boolean trackAppLifecycleEvents = true;
90120

91121
Builder withAppId(final String appId) {
92122
this.appId = appId;
@@ -98,13 +128,13 @@ Builder withRegion(final String region) {
98128
return this;
99129
}
100130

101-
Builder withAutoFlushEventsInterval(final long autoFlushEventsInterval) {
102-
this.autoFlushEventsInterval = autoFlushEventsInterval;
131+
Builder withTrackAppLifecycleEvents(final boolean trackAppLifecycleEvents) {
132+
this.trackAppLifecycleEvents = trackAppLifecycleEvents;
103133
return this;
104134
}
105135

106-
Builder withTrackAppLifecycleEvents(final boolean trackAppLifecycleEvents) {
107-
this.trackAppLifecycleEvents = trackAppLifecycleEvents;
136+
Builder withAutoFlushEventsInterval(final long autoFlushEventsInterval) {
137+
this.autoFlushEventsInterval = autoFlushEventsInterval;
108138
return this;
109139
}
110140

aws-analytics-pinpoint/src/main/java/com/amplifyframework/analytics/pinpoint/PinpointManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ internal class PinpointManager constructor(
7575
encryptedStore,
7676
sharedPrefs,
7777
androidAppDetails,
78-
androidDeviceDetails,
78+
androidDeviceDetails
7979
)
8080
analyticsClient = AnalyticsClient(
8181
context,
8282
awsPinpointConfiguration.autoFlushEventsInterval,
83+
awsPinpointConfiguration.isTrackAppLifecycleEvents,
8384
pinpointClient,
8485
targetingClient,
8586
pinpointDatabase,
8687
sharedPrefs.getUniqueId(),
8788
androidAppDetails,
88-
androidDeviceDetails,
89+
androidDeviceDetails
8990
)
9091
}
9192
}

0 commit comments

Comments
 (0)