## Record event

The Amplify analytics plugin also makes it easy to record custom events within the app. The plugin handles retry logic in the event the device loses network connectivity and automatically batches requests to reduce network bandwidth.

<BlockSwitcher>
<Block name="Java">

```java
AnalyticsEvent event = AnalyticsEvent.builder()
    .name("PasswordReset")
    .addProperty("Channel", "SMS")
    .addProperty("Successful", true)
    .addProperty("ProcessDuration", 792)
    .addProperty("UserAge", 120.3)
    .build();

Amplify.Analytics.recordEvent(event);
```

</Block>
<Block name="Kotlin">

```kotlin
val event = AnalyticsEvent.builder()
    .name("PasswordReset")
    .addProperty("Channel", "SMS")
    .addProperty("Successful", true)
    .addProperty("ProcessDuration", 792)
    .addProperty("UserAge", 120.3)
    .build()

Amplify.Analytics.recordEvent(event)
```

</Block>
<Block name="RxJava">

```java
AnalyticsEvent event = AnalyticsEvent.builder()
    .name("PasswordReset")
    .addProperty("Channel", "SMS")
    .addProperty("Successful", true)
    .addProperty("ProcessDuration", 792)
    .addProperty("UserAge", 120.3)
    .build();

RxAmplify.Analytics.recordEvent(event);
```

</Block>
</BlockSwitcher>

## Flush events

Events have default configuration to flush out to the network every 30 seconds. If you would like to change this, update `amplifyconfiguration.json` with the value in milliseconds you would like for `autoFlushEventsInterval`. This configuration will flush events every 10 seconds:

```json
{
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "analytics": {
        "plugins": {
            "awsPinpointAnalyticsPlugin": {
                "pinpointAnalytics": {
                    "appId": "AppID",
                    "region": "Region",
                    "autoFlushEventsInterval": 10000
                },
                "pinpointTargeting": {
                    "region": "Region"
                }
            }
        }
    }
}
```

To manually flush events, call:

<BlockSwitcher>
<Block name="Java">

```java
Amplify.Analytics.flushEvents();
```

</Block>
<Block name="Kotlin">

```kotlin
Amplify.Analytics.flushEvents()
```

</Block>
<Block name="RxJava">

```java
RxAmplify.Analytics.flushEvents();
```

</Block>
</BlockSwitcher>

## Authentication events

import native_common from "/src/fragments/lib-v1/analytics/native_common/getting-started/auth-events.mdx";

<Fragments fragments={{all: native_common}} />
  
  <BlockSwitcher>
  <Block name="Java">

  ```java
  /**
  * Call this method to log an authentication event to the analytics client.
  */
  public void logAuthenticationEvent() {
    AnalyticsEvent event = AnalyticsEvent.builder()
      .name("_userauth.sign_in")
      .build();
    Amplify.Analytics.recordEvent(event);
  }
  ```

  </Block>
  <Block name="Kotlin">

  ```kotlin

  /**
  * Call this method to log an authentication event to the analytics client.
  */
  fun logAuthenticationEvent() {
    val event = AnalyticsEvent.builder()
      .name("_userauth.sign_in")
      .build()
    Amplify.Analytics.recordEvent(event)
  }
  ```
  </Block>
  <Block name="RxJava">

  ```java

  /**
  * Call this method to log an authentication event to the analytics client.
  */
  public void logAuthenticationEvent() {
    AnalyticsEvent event = AnalyticsEvent.builder()
      .name("_userauth.sign_in")
      .build();
    RxAmplify.Analytics.recordEvent(event);
  }
  ```
  </Block>
  </BlockSwitcher>

## Global Properties

You can register global properties which will be sent along with all invocations of `Amplify.Analytics.recordEvent`.

<BlockSwitcher>
<Block name="Java">

```java
Amplify.Analytics.registerGlobalProperties(
    AnalyticsProperties.builder()
        .add("AppStyle", "DarkMode")
        .build());
```

</Block>
<Block name="Kotlin">

```kotlin
Amplify.Analytics.registerGlobalProperties(
    AnalyticsProperties.builder()
        .add("AppStyle", "DarkMode")
        .build())
```

</Block>
<Block name="RxJava">

```java
RxAmplify.Analytics.registerGlobalProperties(
    AnalyticsProperties.builder()
        .add("AppStyle", "DarkMode")
        .build());
```

</Block>
</BlockSwitcher>

To unregister a global property, call `Amplify.Analytics.unregisterGlobalProperties()`:

<BlockSwitcher>
<Block name="Java">

```java
Amplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty");
```

</Block>
<Block name="Kotlin">

```kotlin
Amplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty")
```

</Block>
<Block name="RxJava">

```java
RxAmplify.Analytics.unregisterGlobalProperties("AppStyle", "OtherProperty");
```

</Block>
</BlockSwitcher>

## Disable Analytics

To disable analytics, call:

<BlockSwitcher>
<Block name="Java">

```java
Amplify.Analytics.disable();
```

</Block>
<Block name="Kotlin">

```kotlin
Amplify.Analytics.disable()
```

</Block>
<Block name="RxJava">

```java
RxAmplify.Analytics.disable();
```

</Block>
</BlockSwitcher>

## Enable Analytics

To re-enable, call:

<BlockSwitcher>
<Block name="Java">

```java
Amplify.Analytics.enable();
```

</Block>
<Block name="Kotlin">

```kotlin
Amplify.Analytics.enable()
```

</Block>
<Block name="RxJava">

```java
RxAmplify.Analytics.enable();
```

</Block>
</BlockSwitcher>

