When you submit a transaction it starts to get propagated to all the network's nodes. After that some miner picks it up at some point and includes in his block. After that the miner may succeed in mining the block with your transaction. After that the block may or may not end up in the canonical chain.
So the possible delays are:
- Transaction propagation through the network. In the beginning only your node has the transaction so it needs to be delivered to everyone else. This takes some time.
- When will a miner include the transaction in a block? This is affected by the
gas price of the tx - the higher price you give the faster miners want to include it (as they get more reward from that tx). Another affecting factor is current network congestion: if there are not many txs in the network miners pick up lower gas priced txs as there's nothing better available.
- When will a miner who included your tx in his block manage to solve the PoW puzzle? It's not enough for your tx to be included in a block but that block also needs to pass the PoW puzzle test. So a miner has to solve the PoW mining problem before he's able to form a block containing your tx.
- When a block with your tx has been mined succesfully the block gets added to the blockchain (and propagated to all nodes again - this also takes time). But someone else may have solved a different block at the same time so your block may end up as an uncle block. Transactions in an uncle block are basically invalidated and the tx needs to be readded to some other block.
To calculate the average time you should probably track all (or at least multiple) transactions from when they are created until they are part of the canonical chain. The biggest affecting factor is the
gas price so making a common average over all transactions may not be very accurate: some go a lot slower and some go a lot faster.
When a block gets added to the blockchain it starts "competing" for being part of the canonical chain. Someone else may have produces a similar block around the same time. Then it's up to the nodes to decide on top of which block they will start mining - they should always choose the heaviest (almost the same as longest) chain they know of. The problem is that they may not know of all the (sub)chains and may therefore start mining on a "wrong" (sub)chain.
The more blocks are produced on top of any given block the more certain it is that the block in question will stay in the canonical chain. But in theory this is never 100% certain: in theory someone could start mining today from the genesis block with different block contents and manage to reach a heavier chain than the current chain.
So when is a block certainly part of the canonical chain? The direct answer is: never.
I'm not sure what kind of ready tools you can use to track tx ending up in the canonical chain as it's never a certainty. But if you run your own node you can for example decide that after 5 blocks the block in question is certain enough to stay in the canonical chain. This is what exchanges call the amount of "confirmations". So all calculations regarding canonical chain depend on the amount of certainty you require.