It's possible to validate/lint/bleach a piece of code given by analytics/tracking sites like Google Analytics or Piwik
The easy way would be to grab the code from the tracking site and hard-code everything but the unique portion (usually a user ID number), offer the user a choice of approved trackers (with a radio button) and have them paste in their ID, then insert that value when you render the page. If I remember correctly, blogger worked like this before they tied it in directly with analytics via the google account.
I provide in the administration interface of the site I make an area for the webmaster to place tracking codes from external analytic tools. Essentially, this codes must be included 'as-is', but my concern is that any typo could render the page useless, mess-up the HTML, etc...
It's possible (at some extent) to cleanup/validate these codes so I least it ensures the HTML won't be corrupted?
I'm using Python/Django, but i guess the Django part is somewhat irrelevant for this topic.
Regards