2022-09-27 17:58:22 +03:00
defmodule Mix.Tasks.SendPageview do
@moduledoc """
It ' s often necessary to generate fake events for development and testing purposes. This Mix Task provides a quick and easy
way to generate a pageview or custom event , either in your development environment or a remote Plausible instance .
See Mix.Tasks.SendPageview . usage / 1 for more detailed documentation .
"""
use Mix.Task
require Logger
2022-10-11 13:13:55 +03:00
@default_host " http://localhost:8000 "
2022-09-27 17:58:22 +03:00
@default_ip_address " 127.0.0.1 "
@default_user_agent " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284 "
@default_domain " dummy.site "
@default_page " / "
@default_referrer " https://google.com "
@options [
ip : :string ,
user_agent : :string ,
domain : :string ,
page : :string ,
2022-10-11 13:13:55 +03:00
referrer : :string ,
host : :string
2022-09-27 17:58:22 +03:00
]
def run ( opts ) do
Finch . start_link ( name : Plausible.Finch )
{ parsed , _ , invalid } = OptionParser . parse ( opts , strict : @options )
case invalid do
[ ] ->
do_send_pageview ( parsed )
[ invalid_option | _ ] ->
{ key , _val } = invalid_option
2022-10-25 14:17:09 +03:00
IO . puts ( " Invalid option #{ key } . Aborting. " )
2022-09-27 17:58:22 +03:00
IO . puts ( usage ( ) )
end
end
defp do_send_pageview ( parsed_opts ) do
ip = Keyword . get ( parsed_opts , :ip , @default_ip_address )
user_agent = Keyword . get ( parsed_opts , :user_agent , @default_user_agent )
2022-10-11 13:13:55 +03:00
host = Keyword . get ( parsed_opts , :host , @default_host )
2022-09-27 17:58:22 +03:00
2022-10-11 13:13:55 +03:00
url = get_url ( host )
2022-09-27 17:58:22 +03:00
headers = get_headers ( ip , user_agent )
body = get_body ( parsed_opts )
case Plausible.HTTPClient . post ( url , headers , body ) do
{ :ok , _ } ->
IO . puts (
2022-10-25 14:17:09 +03:00
" ✅ Successfully sent #{ body [ :name ] } event to #{ url } ✅ \n \n ip= #{ ip } \n user_agent= #{ user_agent } \n body= #{ inspect ( body , pretty : true ) } "
2022-09-27 17:58:22 +03:00
)
{ :error , e } ->
IO . puts ( " ❌ Could not send event to #{ url } . Got the following error: \n \n #{ inspect ( e ) } " )
end
end
2022-10-11 13:13:55 +03:00
defp get_url ( host ) do
Path . join ( host , " /api/event " )
2022-09-27 17:58:22 +03:00
end
defp get_headers ( ip , user_agent ) do
[
{ " x-forwarded-for " , ip } ,
{ " user-agent " , user_agent } ,
{ " content-type " , " text/plain " }
]
end
defp get_body ( opts ) do
domain = Keyword . get ( opts , :domain , @default_domain )
page = Keyword . get ( opts , :page , @default_page )
referrer = Keyword . get ( opts , :referrer , @default_referrer )
%{
name : " pageview " ,
url : " http:// #{ domain } #{ page } " ,
domain : domain ,
referrer : referrer ,
width : 1666
}
end
defp usage ( ) do
"""
usage : $ mix send_pageview [ -- domain domain ] [ -- ip ip_address ] "
options : #{inspect(@options, pretty: true)}
"""
end
end