WealthFit.Campaigns behaviour (Peacemaker v3.3.5-rc.7) View Source

Link to this section Summary

Callbacks

The low-level interface for working with campaigns. This module contains a set a generic methods that will be used when creating new campaigns.

Link to this section Functions

Link to this function

validate_and_set_event_handler(map)

View Source

Link to this section Callbacks

Specs

build(Plug.Conn.t(), Map.t()) :: struct() | {:error, String.t()}

The low-level interface for working with campaigns. This module contains a set a generic methods that will be used when creating new campaigns.

Below is an example of how we could take this behaviour and generic methods to define a new WealthFit.Campaign

    defmodule WealthFit.Campaigns.FreeAccountCampaign  do
      @behaviour WealthFit.Campaigns

      import WealthFit.Formatter
      require Logger

      defstruct [:conn, :payload, :_event_handler]

      @impl WealthFit.Campaigns
      def build(conn, params) do
        case WealthFit.Campaigns.validate_and_set_event_handler(%{offer_id: params["offerId"]}) do
          %{event_handler: event_handler} ->
            %__MODULE__{
              conn: conn,
              payload:
                WealthFit.Campaigns.build_payload_for(params, event_handler)
                |> Map.merge(funnel_specific_attributes(params)),
              _event_handler: event_handler
            }

          {:error, message} ->
            Logger.error(message)
        end
      end

      # --

      def funnel_specific_attributes(formatted_params, raw_params, _event_handler) do
        %{
          campaign_id:
            campaign_id(params) || Application.fetch_env!(:peacemaker, :free_account_campaign_id)
        }
      end
    end

    defimpl WealthFit.Campaigns.Dispatcher, for: WealthFit.Campaigns.FreeAccountCampaign  do
      require Logger
      use WealthFit.Campaigns.Dispatcher.Defaults
    end

The overall idea is that we use validate_and_set_event_handlers/1 to determine what operation to perform based on the given Sticky.io offer id. If no offer id exist, the _event_handler is defaulted to type :optin.

WealthFit.Campaigns.EventHandlerDefinitions contains a list of the available _event_handlers that can be dispatched, with the exception of :optin, since there is no corresponding offer id when creating Sticky.io optins. By default, the WealthFit.Campaigns.CreateOptin module is used as the callback module when dispatching anything with :optin. See WealthFit.Campaigns.Dispatcher.Defaults to learn more about the current default dispatch methods.

Each module that adopts the WealthFit.Campaigns behaviour must also include a dispatch implementation. This is the layer to perform any tasks if we want to override the default behaviour for a given event handler.

Link to this callback

funnel_specific_attributes(arg1, arg2, arg3)

View Source

Specs

funnel_specific_attributes(Map.t(), Map.t(), Atom.t()) :: Map.t()