DTM Way (Recommended way)
Please have a look at my previous reply, I stated it clearly that its the Page where we inject the code by adding could service to the Base page.
"It is always recommended to it at webpage level by going through page property->could service-> select DTM connector"
NON DTM
If it is not with DTM then you can add any script to AEM page in a way with which we add JavaScript normally to the page.
Using DTM for this case is easy, manageable and can be done without knowing the code information.Also, the user could run various kind of rules and make the best use of AEM context hub and custom data layer. This is the main ideology kept in mind so that marketer can configure any analytics JS or third-party JS in a web page.
And if you would like to connect AEM with third party Analytics then this connector could also be used:- Analytics with External Providers
~kautuk