Elixir foundationSort Order

Elixir: Filtering Relevant Network Calls - [019]

We only want to track network calls that are relevant to our page rendering. So things like analytics are irrelevant in this context.

What we'll do in this episode is use =~ operator in elixir to filter out the irrelevant urls.

Elixir =~ Operator

The way the =~ operator works is it compares the left and right hand side and returns true or false if the left hand side contains anything from the right hand side.

"https://www.simpleanalytics.io/post" =~ "https://www.artellectual.com"false
"https://www.artellectual.com/assets/image.png" =~ "https://www.artellectual.com"true

So what we can do is use this to check the url and selectively track the url.

We can now modify our GenServer

def init(url) do  # ...  {:ok, %{    conn: conn,     requests: requests,     origin: "https://artellectual.com"  }}end

Now in the handle_info callback

def handle_info(  {:chrome_remote_interface, "Network.requestWillBeSent", data},   state) do  url = data["params"]["request"]["url"]     if url =~ state.origin do    Agent.update(state.requests, fn state -> state ++ [url] end)  end    {:noreply, state}end

That's pretty much it! We should now only be tracking urls relevant to our cause!