Skip to content

Instantly share code, notes, and snippets.

@paridin
Last active June 23, 2020 20:23
Show Gist options
  • Save paridin/4216a2e871557a2fcc37690b70794fa5 to your computer and use it in GitHub Desktop.
Save paridin/4216a2e871557a2fcc37690b70794fa5 to your computer and use it in GitHub Desktop.
an example of create an pixel event using elixir
defmodule Fb.Pixel do
@moduledoc """
Facebook Pixel
requires HTTPPoison, Jason
"""
@pixel_id Application.get_env(:fb, :pixel_id)
@api_version Application.get_env(:fb, :fb_graph_api_version)
def create_events(token, events, test_event \\ nil) do
test_event = case test_event do
nil -> []
event -> [test_event_code: event]
end
params =
[
params: [
data: Jason.encode!(events),
access_token: access_token,
] ++ test_event
]
HTTPoison.post(
"https://graph.facebook.com/#{@api_version}/#{@pixel_id}/events",
"",
[],
params
)
end
end
# example usage, please notice is our :fb Application OTP, so you should put in your own :my_app or whatever.
# config: :fb,
# pixel_id: "pixel_id_get_from_fb"
# pixel_access_token: "get_from_fb"
# fb_graph_api_version: "v7.0"
# email_hash = :sha256 |> :crypto.hash("dev@paridin.com") |> Base.encode16(case: :lower)
# events = [
# %{
# "event_name" => "Purchase",
# "event_time" => 1_592_937_775,
# "user_data" => %{
# "em" => email_hash,
# "ph" => nil
# },
# "custom_data" => %{
# "currency" => "USD",
# "value" => "142.52"
# }
# }
# ]
# events = [
# %{
# "event_name" => "InitiateCheckout",
# "event_time" => 1_592_937_779,
# "user_data" => %{
# "client_ip_address" => "10.10.1.1",
# "client_user_agent" => "Firefox"
# },
# "custom_data" => %{
# "visiting" => "MEXICO",
# "session_id" => "435sdvsdfXd2"
# }
# }
# ]
# token = Application.get_env(:fb, :pixel_access_token)
# Fb.Pixel.create_event(token, events)
# Fb.Pixel.create_event(token, events, "TEST001")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment