DonJoe
DonJoe

Reputation: 1763

Can I put analytics script in a javascript file

When we get different script, like for google analytics, adsense, or facebook code etc, generally there are 2 scripts.

The first one is an external script tag and the second one some javascript code.

My question: Which is the best way to include these scripts (performance-related)?

I was thinking of including

<script src="//analytics.com/" async defer></script>"` 

in <head> , and the js code in a js file, say, main.js, so that it can be cached and minimized with the rest of the javascript.

Would there be any disadvantages of doing this?

If not, (if this is actually better), why everybody is suggesting to put the scripts inside <body> ?

Upvotes: 0

Views: 94

Answers (2)

jack
jack

Reputation: 1391

To just answer the question:

yes.

Is it better?

Well, kind of. It will be cached, minimized , which is not a bad thing.

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

If you have multiple 3rd-party scripts its easier to use Google Tag Manager to handle this for you. You add the tag manager script to your page, and it takes care of injecting whatever you want.

Upvotes: 0

Related Questions