问题
My Options :
include the analytics code in page templates or CTs.
create a component contains the analytic code and render it in Comonent presentation of the page.
which could be better or please suggest any other way out.
回答1:
The first approach is normally preferable, since it allows you to include information about a component or page in your call to Google Analytics. And you may not even have to write it yourself, since there is a free extension called the Web Analytics Enabler, which is available here. I recommend that you check that out first.
回答2:
For analytics or any markup you're adding with SDL Tridion, you'll want to confirm the content model, authors, and consider how the content and templates may evolve.
Content Model
Assuming a single instance across all pages, you can add (per Google) to your page template:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Authors
Authors would be developers in this case. But if you have SEO authors and multiple publications, consider adding the script via template, but the tracking code in a component field, referenced in your layout TBB:
_gaq.push(['_setAccount', '@@GoogleTrackingCode@@']);
Up to you on how to add GoogleTrackingCode to the package. Adjust as needed to your rendering framework or templating approach.
Changes
If the script format may change frequently (doubt it), consider component fields for the script itself. It's more likely the business will want more-detailed link and campaign type tracking.
More-Detailed Tracking
To add tracking codes for individual links (Outbound links) or custom tracking, place either at the page template (for non-dyanamic embedded component presentations) or from CTs. Just remember components aren't unique on their own.
来源:https://stackoverflow.com/questions/12068311/how-to-implement-google-analytics-in-sdl-tridion-web-sites