Package com.mparticle.commerce
Class CommerceEvent.Builder
- java.lang.Object
-
- com.mparticle.commerce.CommerceEvent.Builder
-
- Enclosing class:
- CommerceEvent
public static class CommerceEvent.Builder extends java.lang.Object
The Builder class forCommerceEvent
. Use this class to create immutable CommerceEvents that can then be logged. There are 3 types ofCommerceEvent
:
This class provides a constructor for each type and will verify the contents of the event whenbuild()
is called.
Sample Product event
Product product = new Product.Builder("Foo name", "Foo sku", 100.00).quantity(4).build(); CommerceEvent event = new CommerceEvent.Builder(Product.PURCHASE, product) .transactionAttributes(new TransactionAttributes().setId("bar-transaction-id") .setRevenue(product.getTotalAmount())) .build(); MParticle.getInstance().logEvent(event);
Sample Promotion event
Promotion promotion = new Promotion().setCreative("foo-creative").setName("bar campaign"); CommerceEvent event = new CommerceEvent.Builder(Promotion.VIEW, promotion).build(); MParticle.getInstance().logEvent(event);
Sample Impression event
Product product = new Product.Builder("Foo name", "Foo sku", 100.00).quantity(4).build(); Impression impression = new Impression("foo-list-name", product); CommerceEvent event = new CommerceEvent.Builder(impression).build(); MParticle.getInstance().logEvent(event);
-
-
Constructor Summary
Constructors Constructor Description Builder(CommerceEvent event)
Convenience copy-constructor.Builder(Impression impression)
Constructor for aImpression
-based CommerceEvent.Builder(java.lang.String productAction, Product product)
Constructor for aProduct
-based CommerceEvent.Builder(java.lang.String promotionAction, Promotion promotion)
Constructor for aPromotion
-based CommerceEvent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CommerceEvent.Builder
addCustomFlag(java.lang.String key, java.lang.String value)
Add a custom flag to this event.CommerceEvent.Builder
addImpression(Impression impression)
Add aImpression
to this CommerceEvent.CommerceEvent.Builder
addProduct(Product product)
Add aProduct
to this CommerceEvent.CommerceEvent.Builder
addPromotion(Promotion promotion)
Add aPromotion
to this CommerceEvent.CommerceEvent
build()
Create the CommerceEvent.CommerceEvent.Builder
checkoutOptions(java.lang.String options)
Set custom options to be associated with the event.CommerceEvent.Builder
checkoutStep(java.lang.Integer step)
Set the checkout step of this event.CommerceEvent.Builder
currency(java.lang.String currency)
Set the ISO 4217 currency code to associate with this event.CommerceEvent.Builder
customAttributes(java.util.Map<java.lang.String,java.lang.String> attributes)
Associate a Map of custom attributes with this event.CommerceEvent.Builder
customFlags(java.util.Map<java.lang.String,java.util.List<java.lang.String>> customFlags)
Bulk add custom flags to this event.CommerceEvent.Builder
impressions(java.util.List<Impression> impressions)
Overwrite the Impressions associated with the CommerceEvent.CommerceEvent.Builder
internalEventName(java.lang.String eventName)
Private API used internally by the SDK.CommerceEvent.Builder
nonInteraction(boolean userTriggered)
Set this CommerceEvent to be measured as non-user-triggered.CommerceEvent.Builder
productListName(java.lang.String listName)
Set the list name with the Products of the CommerceEvent.CommerceEvent.Builder
productListSource(java.lang.String listSource)
Set the list source name with the Products of the CommerceEvent.CommerceEvent.Builder
products(java.util.List<Product> products)
Overwrite the Products associated with the CommerceEvent.CommerceEvent.Builder
promotions(java.util.List<Promotion> promotions)
Overwrite the Promotions associated with the CommerceEvent.CommerceEvent.Builder
screen(java.lang.String screenName)
Set the screen to associate with this event.CommerceEvent.Builder
shouldUploadEvent(boolean shouldUploadEvent)
Manually choose to skip uploading this event to mParticle server and only forward to kits.CommerceEvent.Builder
transactionAttributes(TransactionAttributes attributes)
Associate aTransactionAttributes
object with this event.
-
-
-
Constructor Detail
-
Builder
public Builder(@NonNull java.lang.String productAction, Product product)
Constructor for aProduct
-based CommerceEvent.- Parameters:
productAction
- a String representing the action that the user performed. This must be one of the String constants defined by theProduct
class. Must not be null.product
- atProduct
object to associate with the given action. Must not be null.- See Also:
Product.CLICK
,Product.DETAIL
,Product.CHECKOUT
,Product.ADD_TO_CART
,Product.REMOVE_FROM_CART
,Product.ADD_TO_WISHLIST
,Product.REMOVE_FROM_WISHLIST
,Product.CHECKOUT
,Product.CHECKOUT_OPTION
,Product.PURCHASE
-
Builder
public Builder(@NonNull java.lang.String promotionAction, @NonNull Promotion promotion)
Constructor for aPromotion
-based CommerceEvent.- Parameters:
promotionAction
- a String representing the action that use user performed. This must be on the String constants defined by thePromotion
class. Must not be null.promotion
- at least 1Promotion
object to associate with the given action. Must not be null.- See Also:
Promotion.CLICK
,Promotion.VIEW
-
Builder
public Builder(@NonNull Impression impression)
Constructor for aImpression
-based CommerceEvent.- Parameters:
impression
- the impression to associate with this event. Must not be null.
-
Builder
public Builder(@NonNull CommerceEvent event)
Convenience copy-constructor. Use this to convert or mutate a given CommerceEvent.- Parameters:
event
- an existing CommerceEvent. Must not be null.
-
-
Method Detail
-
screen
@NonNull public CommerceEvent.Builder screen(@Nullable java.lang.String screenName)
Set the screen to associate with this event.- Parameters:
screenName
- a String name or description of the screen where this event occurred.- Returns:
- returns this Builder for easy method chaining.
-
addProduct
@NonNull public CommerceEvent.Builder addProduct(Product product)
Add aProduct
to this CommerceEvent. This should only be called forProduct
-based CommerceEvents created withBuilder(java.lang.String, com.mparticle.commerce.Product)
.- Parameters:
product
- theProduct
to add to this CommerceEvent.- Returns:
- returns this Builder for easy method chaining.
-
transactionAttributes
@NonNull public CommerceEvent.Builder transactionAttributes(@NonNull TransactionAttributes attributes)
Associate aTransactionAttributes
object with this event. This will typically be used withProduct
-based CommerceEvents. ForProduct.PURCHASE
andProduct.REFUND
CommerceEvents, this is required to be set.- Parameters:
attributes
- theTransactionAttributes
object- Returns:
- returns this Builder for easy method chaining.
-
currency
@NonNull public CommerceEvent.Builder currency(@Nullable java.lang.String currency)
Set the ISO 4217 currency code to associate with this event.- Parameters:
currency
- an ISO 4217 String- Returns:
- returns this Builder for easy method chaining.
-
nonInteraction
@NonNull public CommerceEvent.Builder nonInteraction(boolean userTriggered)
Set this CommerceEvent to be measured as non-user-triggered.- Parameters:
userTriggered
- a Boolean indicating if a user actually performed this event- Returns:
- returns this Builder for easy method chaining.
-
customAttributes
@NonNull public CommerceEvent.Builder customAttributes(@Nullable java.util.Map<java.lang.String,java.lang.String> attributes)
Associate a Map of custom attributes with this event.- Parameters:
attributes
- the Map of attributes- Returns:
- returns this Builder for easy method chaining.
-
addCustomFlag
@NonNull public CommerceEvent.Builder addCustomFlag(@Nullable java.lang.String key, @Nullable java.lang.String value)
Add a custom flag to this event. Flag keys can have multiple values - if the provided flag key already has an associated value, the value will be appended.- Parameters:
key
- (required) a flag key, retrieve this from the mParticle docs or solution team for your intended services(s)value
- (required) a flag value to be send to the service indicated by the flag key- Returns:
- returns this builder for easy method chaining
-
customFlags
@NonNull public CommerceEvent.Builder customFlags(@Nullable java.util.Map<java.lang.String,java.util.List<java.lang.String>> customFlags)
Bulk add custom flags to this event. This will replace any flags previously set viaaddCustomFlag(String, String)
- Parameters:
customFlags
- (required) a map containing the custom flags for the CommerceEvent- Returns:
- returns this builder for easy method chaining
-
addPromotion
@NonNull public CommerceEvent.Builder addPromotion(@NonNull Promotion promotion)
Add aPromotion
to this CommerceEvent. This should only be called forPromotion
-based CommerceEvents created withBuilder(java.lang.String, com.mparticle.commerce.Promotion)
- Parameters:
promotion
- thePromotion
to add to this CommerceEvent.- Returns:
- returns this Builder for easy method chaining.
-
checkoutStep
@NonNull public CommerceEvent.Builder checkoutStep(@Nullable java.lang.Integer step)
Set the checkout step of this event. Should be used with theProduct.CHECKOUT
andProduct.CHECKOUT_OPTION
actions.- Parameters:
step
- the Integer step- Returns:
- returns this Builder for easy method chaining.
-
addImpression
@NonNull public CommerceEvent.Builder addImpression(@NonNull Impression impression)
Add aImpression
to this CommerceEvent. This should only be called forImpression
-based CommerceEvents created withBuilder(com.mparticle.commerce.Impression)
- Parameters:
impression
- theImpression
to add to the CommerceEvent.- Returns:
- returns this Builder for easy method chaining.
-
checkoutOptions
@NonNull public CommerceEvent.Builder checkoutOptions(@Nullable java.lang.String options)
Set custom options to be associated with the event. Should be used with theProduct.CHECKOUT
andProduct.CHECKOUT_OPTION
actions.- Parameters:
options
- a String describing this checkout step- Returns:
- returns this Builder for easy method chaining.
-
build
@NonNull public CommerceEvent build()
Create the CommerceEvent. This method should be called when all of your desired datapoints have been added. When the SDK is inMParticle.Environment.Development
mode, this method will throw anIllegalStateException
if you have created an invalid CommerceEvent, such as by combiningProduct
objects withPromotion
objects. When inMParticle.Environment.Production
mode, errors will be logged to the console.- Returns:
- returns the resulting immutable CommerceEvent to be logged
- See Also:
MParticle.logEvent(com.mparticle.commerce.CommerceEvent)
-
productListName
@NonNull public CommerceEvent.Builder productListName(@Nullable java.lang.String listName)
Set the list name with the Products of the CommerceEvent. This value should be used with theProduct.DETAIL
andProduct.CLICK
actions.- Parameters:
listName
- a String name identifying the product list- Returns:
- returns this Builder for easy method chaining.
-
productListSource
@NonNull public CommerceEvent.Builder productListSource(@Nullable java.lang.String listSource)
Set the list source name with the Products of the CommerceEvent. This value should be used with theProduct.DETAIL
andProduct.CLICK
actions.- Parameters:
listSource
- a String name identifying the product's list source- Returns:
- returns this Builder for easy method chaining.
-
products
@NonNull public CommerceEvent.Builder products(@NonNull java.util.List<Product> products)
Overwrite the Products associated with the CommerceEvent. This should only be used withProduct
-based CommerceEvents created withBuilder(java.lang.String, com.mparticle.commerce.Product)
- Parameters:
products
- the List of products to associate with the CommerceEvent- Returns:
- returns this Builder for easy method chaining.
-
impressions
@NonNull public CommerceEvent.Builder impressions(@NonNull java.util.List<Impression> impressions)
Overwrite the Impressions associated with the CommerceEvent. This should only be used withImpression
-based CommerceEvents created withBuilder(com.mparticle.commerce.Impression)
- Parameters:
impressions
- the List of products to associate with the CommerceEvent- Returns:
- returns this Builder for easy method chaining.
-
promotions
@NonNull public CommerceEvent.Builder promotions(@NonNull java.util.List<Promotion> promotions)
Overwrite the Promotions associated with the CommerceEvent. This should only be used withPromotion
-based CommerceEvents created withBuilder(java.lang.String, com.mparticle.commerce.Promotion)
- Parameters:
promotions
- the List of products to associate with the CommerceEvent- Returns:
- returns this Builder for easy method chaining.
-
internalEventName
@NonNull public CommerceEvent.Builder internalEventName(@Nullable java.lang.String eventName)
Private API used internally by the SDK.
-
shouldUploadEvent
@NonNull public CommerceEvent.Builder shouldUploadEvent(boolean shouldUploadEvent)
Manually choose to skip uploading this event to mParticle server and only forward to kits. Note that if this method is not called, the default is to upload to mParticle as well as forward to kits to match the previous behavior.- Parameters:
shouldUploadEvent
-- Returns:
- returns this builder for easy method chaining
-
-