com.segment.analytics.messages.PageMessage Maven / Gradle / Ivy
package com.segment.analytics.messages;
import com.google.auto.value.AutoValue;
import com.segment.analytics.gson.AutoGson;
import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;
/**
* The page call lets you record whenever a user sees a page of your website, along with any
* properties about the page.
*
* Use {@link #builder} to construct your own instances.
*
* @see Page
*/
@AutoValue
@AutoGson //
public abstract class PageMessage implements Message {
/**
* Start building an {@link PageMessage} instance.
*
* @param name The name of the page the user is on.
* @throws IllegalArgumentException if the page name is null or empty
* @see Page
*/
public static Builder builder(String name) {
return new Builder(name);
}
public abstract String name();
@Nullable
public abstract Map properties();
@Nullable
public abstract String category();
public Builder toBuilder() {
return new Builder(this);
}
/** Fluent API for creating {@link PageMessage} instances. */
public static class Builder extends MessageBuilder {
private String name;
private Map properties;
private String category;
private Builder(PageMessage page) {
super(page);
name = page.name();
properties = page.properties();
category = page.category();
}
private Builder(String name) {
super(Type.page);
if (isNullOrEmpty(name)) {
throw new IllegalArgumentException("page name cannot be null or empty.");
}
this.name = name;
}
/**
* Set a map of information that describe the page. These can be anything you want.
*
* @see Properties
*/
public Builder properties(Map properties) {
if (properties == null) {
throw new NullPointerException("Null properties");
}
this.properties = ImmutableMap.copyOf(properties);
return this;
}
public Builder category(String category) {
if (category == null) {
throw new NullPointerException("Null category");
}
this.category = category;
return this;
}
@Override
Builder self() {
return this;
}
@Override
protected PageMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map context,
String anonymousId,
String userId,
Map integrations) {
return new AutoValue_PageMessage(
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
userId,
integrations,
name,
properties,
category);
}
}
}