This number could be much more easily collected by reading out the total fees collected in a block and dividing it by the count of transactions in that block (deducting one for the coinbase), then translating the resulting average fee (BTC) to a USD amount.
However, I'm going to offer a frame challenge. I do not consider “average fee per transaction (USD)” a useful metric.
First of all, it would be more meaningful, if the data were collected as “average fee per transaction (BTC)” as it would remove the overlying effect of the current exchange rate from the blockspace demand situation. Secondly, the fees shift wildly over the course of the day and week, so how useful is an average even? If you want to pay less, you can get away with the minimum feerate, if you are fine with waiting for a day ~12h. So it should be cast as “average fee to get confirmation within what time”? Thirdly, a transaction that pays 100 recipients, a transaction with a single recipient, and a transaction consolidating two hundred inputs in a self-send are interpreted all the same for the calculation of this figure. If you e.g. consider the four example blocks shown here, you would get wildly different average fees—but mostly because users are trying to achieve wildly different things. Fourthly, transactions pay for blockspace, but the average value of payments on Bitcoin has increased by magnitudes over the project's existence. How comparable are costs per transaction over the years, when it meant sending $1 in average a decade ago and now the average payment is more than $10,000?
What you're trying to express with that metric? Is it a measure of how expensive it is currently to purchase blockspace? That's just the current feerate. Is it a measure of how expensive it is to transfer a certain amount of value? That depends on the user's UTXO pool and their urgency, whether they're sending to a single recipient or batching payments.
Perhaps it would be more interesting to offer figures on what the cost of getting a specific transaction confirmed in various timeframes would be. E.g. a transaction with one P2WPKH input and two P2WPKH outputs is 140.5 vB, and present what it costs to get that confirmed within the next hour vs the next day.