View Source SauceAnalytics (sauce_analytics v0.1.1)

GenServer which is responsible for managing client state and sending requests to the Analytics API.

The plug SauceAnalytics.Plug.MaintainSession is required to run before any function invocations in this module because it depends on keys created in Plug.Session.

options

Options

  • :app_info - SauceAnalytics.AppInfo which idenfies the app on the analytics API, ensure the name of the app is unique. Required.
  • :api_url - The URL of the Sauce Analytics API which will be used for visit and event requests. Required.
  • :session_id_name - The name of the key which will be used in the Plug.Session for storing the session's id. Defaults to :sauce_analytics_session_id.
  • :revive_session_name - The name of the key which will be used in the Plug.Session and socket.assigns for storing the SauceAnalytics.ReviveSession struct. Defaults to :sauce_analytics_session_revive_info.
  • :revive_session_cookie_name - The name of the cookie used for storing the SauceAnalytics.ReviveSession struct. Defaults to "sauce_analytics_session_revive_info".

Link to this section Summary

Functions

Given a conn, The uid in the store is updated with given user_id and the uid in revive_session stored in the Plug.Session is updated with the given user_id.

Returns a specification to start this module under a supervisor.

Returns the state/configuration of the SauceAnalytics GenServer.

Starts the SauceAnalytics GenServer with the given opts

Given a conn or a socket with session information. An event request is sent to the Analytics API.

Given a conn or a socket with session information. A visit request is sent to the Analytics API.

Link to this section Types

@type opts() ::
  {:session_id_name, atom()}
  | {:revive_session_name, atom()}
  | {:revive_session_cookie_name, String.t()}
  | {:app_info, SauceAnalytics.AppInfo.t()}
  | {:api_url, String.t()}

Link to this section Functions

Link to this function

assign_user(conn, user_id)

View Source
@spec assign_user(conn :: Plug.Conn.t(), user_id :: String.t() | nil) :: Plug.Conn.t()

Given a conn, The uid in the store is updated with given user_id and the uid in revive_session stored in the Plug.Session is updated with the given user_id.

user_id can be nil.

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec get_state() :: SauceAnalytics.State.t()

Returns the state/configuration of the SauceAnalytics GenServer.

@spec start_link([opts()]) :: GenServer.on_start()

Starts the SauceAnalytics GenServer with the given opts

Use this in your application supervision tree.

Link to this function

track_event(conn_or_socket, name, title, data \\ nil)

View Source
@spec track_event(
  conn_or_socket :: Plug.Conn.t() | Phoenix.LiveView.Socket.t(),
  name :: String.t(),
  title :: String.t(),
  data :: map() | nil
) :: Plug.Conn.t() | Phoenix.LiveView.Socket.t()

Given a conn or a socket with session information. An event request is sent to the Analytics API.

example

Example

Originally intended for Ember, the arguments would be:

  • name - A unique string to represent the event (e.g "users.search")
  • title -A unique string to represent the route the user has visited (e.g "internal.list.users")
  • data - A JSON object containing any data related to the event (e.g {"term": "John", results: [...]})

However there is no concept of routes in Phoenix so this would be sufficient:

  • name - A unique string to represent the event (e.g "users.search")
  • title - A full URL without query params (e.g "/list/users")
  • data - A map with string keys containing any data related to the event (e.g %{"term" => "John", "results" => [...])

liveview

LiveView

defmodule MyApp.Live.ListUsers do
  use MyApp, :live_view

  # required for any analytics calls in a LiveView
  on_mount(SauceAnalytics.Live.SetupAssigns)

  def mount(...), do: ...

  def handle_event("search", data, socket) do
    ...

    {:noreply,
    socket
    |> SauceAnalytics.track_event("/users/list", "users.list.search",
      %{"count" => length(data.results),
        "results" => data.results}
    )}
  end
end
Link to this function

track_visit(conn_or_socket, name, title)

View Source
@spec track_visit(
  conn_or_socket :: Plug.Conn.t() | Phoenix.LiveView.Socket.t(),
  name :: String.t(),
  title :: String.t()
) :: Plug.Conn.t() | Phoenix.LiveView.Socket.t()

Given a conn or a socket with session information. A visit request is sent to the Analytics API.

example

Example

Originally intended for Ember, the arguments would be:

  • name - A full URL including route/query params (e.g. "/list/users?sortBy=title")
  • title - A unique string to represent the route the user has visited (e.g. "internal.list.users")

However there is no concept of routes in Phoenix so this would be sufficient:

  • name - A full URL including query params (e.g "/list/users?sortBy=title")
  • title - A full URL without query params (e.g "/list/users")