# Avo Inspector Go SDK

## Quick Start Guide

Find the Quick Start Guide in our [GitHub repo](https://github.com/avohq/go-avo-inspector/tree/develop).

## Installation

Our library is distributed as a Go module:

```bash
go get github.com/avohq/go-avo-inspector
```

Use the latest GitHub release tag to get the latest version of the library.

## Import

```go
import (
	avoinspector "github.com/avohq/go-avo-inspector"
)
```

## Initialization

Obtain the API key by opening your [Avo.app workspace settings](https://www.avo.app/schemas/default/settings), selecting the source you want to add and then click the _Inspector Setup_ tab.

You will need to create an instance of `AvoInspector`.

```go
func NewAvoInspector(
	apiKey string,
	env AvoInspectorEnv,
	appVersion string,
	appName string
) (*AvoInspector, error)
```

> #### Parameters:
>
> - `apiKey string` - the API key you get in Inspector tab of your Avo workspace
> - `env AvoInspectorEnv` - current environment: AvoInspectorEnv.Dev, AvoInspectorEnv.Staging or AvoInspectorEnv.Prod
> - `appVersion string` - app version to attribute the events to
> - `appName string` - application name

## Sending event schemas to Avo Inspector

This is the core of the **Avo Inspector SDK**.

Call the following method every time an event is tracked, so Inspector can analyze the event's schema and spot problems.

### Option 1

```go
func (inspector *AvoInspector) TrackSchemaFromEvent(
	eventName string,
	eventProperties map[string]interface{}
) ([]Property, error)
```

> Example usage:
>
> ```go
> func trackEvent(eventName string, eventParams map[string]interface{}) {
>     tracker.Track(eventName, eventParams)
>     avoInspector.TrackSchemaFromEvent(eventName, eventParams)
> }
> ```

Extracts event schema from event properties represented by the second parameter `map[string]interface{}` and sends the schema to **Avo** for analysis.

> #### Parameters:
>
> - `eventName string` - event name, sometimes referred as event type.
> - `eventProperties map[string]interface{}` - actual event properties, which will be converted to event schema on the device and the event schema will be sent to **Avo**. Resulting keys will be JSON fields keys and resulting values will be JSON fields values types converted to schema types.
>
> ##### Example format:
>
```go
 map[string]interface{}{
	"greeting":  "hello",
	"answer":  42,
}
```

> #### Return Type:
>
> - `[]avoinspector.Property` containing event schema, so you can verify that conversion was correct.
>   Example format:
>
```go
[]Property{
	{
		PropertyName: "greeting",
		PropertyType: "string",
		Children:     nil,
	},
	{
		PropertyName: "answer",
		PropertyType: "int",
		Children:     nil,
	}
}
```

See [this](https://www.avo.app/docs/reference/avo-inspector-sdks/overview.md#standalone-usage) for details about event schema structure and schema types.

## Other methods

### 1. Print logs

```go
func (c *AvoInspector) ShouldLog(shouldLog bool)
```

`shouldLog` controls printing of tracked event schemas and other helpful information to logcat. Enabled by default in development environments.

> #### Parameters:
>
> - `shouldLog bool` - sets whether **Avo Inspector SDK** will print logs to console.
