# Album Plugin:	format/google_analytics
# For info:	'album -plugin_info format/google_analytics'
# For usage:	'album -plugin_usage format/google_analytics'

use strict;

my $DESCRIPTION = << 'DESCRIPTION';
Plugin: format/google_analytics

Adds google_analytics tags to the <head> section of album pages.
Uses the latest version of the tracking id.

See:  http://google.com/analytics/

You need to specify your tracking id to the plugin with something like:

% album -google_analytics:id UA-XXXXXXX-X

The tracking id can be found embedded in the tracking code that
google gives you, probably on a line that looks like this:

  var pageTracker = _gat._getTracker("UA-XXXXXXX-X");

It's the part inside the quotes that the plugin needs.

According to google:  "Where can I find my tracking code?"
   https://www.google.com/support/googleanalytics/bin/answer.py?answer=55603

   1. Sign in to Google Analytics
   2. From the Analytics Settings page, find the profile for which you 
      would like to retrieve the tracking code. Please note that tracking 
      code is profile-specific.
   3. From that profile's Settings column, click Edit
   4. At the top right of the Main Website Profile Information box, 
      click Check Status
   5. Your tracking code can be found in the text box in 
      the Instructions for adding tracking section

Issues about tracking?  Don't ask me, ask google:
   https://www.google.com/support/googleanalytics/bin/answer.py?answer=75129

Google may update/change their code, in which case this plugin
will no longer work without being updated!  If you find that the
code this plugin generates is out-of-date, make sure you have the
most current version of album and it's plugins from:

  http://MarginalHacks.com/

And if so, please contact us and let us know!

DESCRIPTION

sub start_plugin {
	my ($opt) = @_;

	# Setup the options
	album::add_option(1, 'id', album::OPTION_STR,
		usage=>"The tracking ID for a profile\n\n".$DESCRIPTION);

	# Setup the hooks
	album::hook($opt, 'do_album', \&setup_head);

	return {
		author => 'David Ljung Madison',
		href => 'http://MarginalHacks.com/',
		version => '1.0',
		description => $DESCRIPTION,
	};
}

sub setup_head {
	my ($opt,$data, undef,  $dir, $album) = @_;

	my $id = album::option($opt, 'id');

	album::usage($opt,"Plugin google_analytics needs you to specify:\n  -google_analytics:id")
		unless $id;

	# Add it to the head
	$data->{head} .= <<ANALYTICS;

		<!-- Code for http://google.com/analytics/   -->
    <!-- Added by plugin format/google_analytics -->
    <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
      var pageTracker = _gat._getTracker("UA-5251996-1");
      pageTracker._trackPageview();
    </script>
		<!-- END code for http://google.com/analytics/   -->
ANALYTICS

	# Still do the album!
	return 0;
}

# Plugins always end with:
1;
