Class MessageBuilder<T extends Message,​V extends MessageBuilder>

java.lang.Object
com.segment.analytics.messages.MessageBuilder<T,​V>
Direct Known Subclasses:
AliasMessage.Builder, GroupMessage.Builder, IdentifyMessage.Builder, PageMessage.Builder, ScreenMessage.Builder, TrackMessage.Builder

public abstract class MessageBuilder<T extends Message,​V extends MessageBuilder>
extends Object
Fluent API to construct instances of a Message.

Note: Although it is not enforced by the compiler, either the Message.anonymousId() or Message.userId() must be provided before calling build(). Failure to do so will result in a IllegalStateException at runtime.

  • Method Details

    • messageId

      public V messageId​(UUID messageId)
      Deprecated.
      Use messageId(String) instead.
      The Message ID is a unique identifier for each message. If not provided, one will be generated for you. This ID is typically used for deduping - messages with the same IDs as previous events may be dropped.
      See Also:
      Common Fields
    • messageId

      public V messageId​(String messageId)
      The Message ID is a unique identifier for each message. If not provided, one will be generated for you. This ID is typically used for deduping - messages with the same IDs as previous events may be dropped.
      See Also:
      Common Fields
    • timestamp

      public V timestamp​(Date timestamp)
      Set a timestamp for the event. By default, the current timestamp is used, but you may override it for historical import.
      See Also:
      Timestamp
    • context

      public V context​(Map<String,​?> context)
      Set a map of information about the state of the device. You can add any custom data to the context dictionary that you'd like to have access to in the raw logs.

      Some keys in the context dictionary have semantic meaning and will be collected for you automatically, depending on the library you send data from. Some keys, such as location and speed need to be manually entered.

      See Also:
      Context
    • anonymousId

      public V anonymousId​(UUID anonymousId)
      Deprecated.
      Use anonymousId(String) instead.
      The Anonymous ID is a pseudo-unique substitute for a User ID, for cases when you don’t have an absolutely unique identifier.
      See Also:
      Identities, Anonymous ID
    • anonymousId

      public V anonymousId​(String anonymousId)
      The Anonymous ID is a pseudo-unique substitute for a User ID, for cases when you don’t have an absolutely unique identifier.
      See Also:
      Identities, Anonymous ID
    • userId

      public V userId​(String userId)
      The User ID is a persistent unique identifier for a user (such as a database ID).
      See Also:
      Identities, User ID
    • enableIntegration

      public V enableIntegration​(String key, boolean enable)
      Set whether this message is sent to the specified integration or not. 'All' is a special key that applies when no key for a specific integration is found.
      See Also:
      Integrations
    • integrationOptions

      public V integrationOptions​(String key, Map<String,​?> options)
      Pass in some options that will only be used by the target integration. This will implicitly mark the integration as enabled.
      See Also:
      Integrations
    • realBuild

      protected abstract T realBuild​(Message.Type type, String messageId, Date timestamp, Map<String,​?> context, String anonymousId, String userId, Map<String,​Object> integrations)
    • build

      public T build()
      Create a Message instance.
      Throws:
      IllegalStateException - if both anonymousId and userId are not provided.
    • type

      public Message.Type type()
      Returns the Message.Type of the message this builder is constructing.