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

# Version history:
# v1.00 David Ljung Madison Stellar <contactRequest at GetDave.com>
#       + released


use strict;

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

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

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

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

% album -google_analytics4:id G-XXXXXXXXXX

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

  gtag('config', 'G-XXXXXXXXXX');

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_analytics4 needs you to specify:\n  -google_analytics4:id")
		unless $id;

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

		<!-- Code for http://analytics.google.com/   -->
    <!-- Added by plugin format/google_analytics4 -->
		<script async src="https://www.googletagmanager.com/gtag/js?id=$id"></script>
		<script>
  		window.dataLayer = window.dataLayer || [];
  		function gtag(){dataLayer.push(arguments);}
  		gtag('js', new Date());
		
  		gtag('config', '$id');
		</script>
		<!-- END code for http://analytics.google.com/   -->
ANALYTICS

	# Still do the album!
	return 0;
}

# Plugins always end with:
1;
