use_google_analytics.naml

<macro name="use_google_analytics" requires="servlet">
	<n.if.not.visitor.is_site_admin>
		<then>
			<n.login.><t>Only authorized users can proceed in this area.</t></n.login.>
		</then>
	</n.if.not.visitor.is_site_admin>

	<n.if.is_submitted_form>
		<then>
			<n.catch_exception. id="save-block">
				<n.if.is_valid_analytics_id.google_analytics_code_field.value>
					<then>
						<n.save_google_analytics_tweak/>
						<n.redirect_to.root_node.url/>
					</then>
					<else.throw_template_exception name="invalid_analytics_id"/>
				</n.if.is_valid_analytics_id.google_analytics_code_field.value>
			</n.catch_exception.>
		</then>
		<else>
			<n.google_analytics_code_field.set_value value="[n.naml_configuration.get_value name='googleAnalyticsId' default=''/]"/>
		</else>
	</n.if.is_submitted_form>

	<n.html>
		<head>
			<meta name="robots" content="noindex,nofollow"/>
			<n.title.><t>Use Google Analytics</t></n.title.>
			<n.google_analytics_code_field.focus/>
		</head>
		<body>
			<n.edit_header first_text="[n.root_node.subject/]" second_text="[t]Use Google Analytics[/t]" />

			<n.if.both condition1="[n.is_submitted_form/]" condition2="[n.has_exception for='save-block'/]">
				<then>
					<n.format_error.handle_exception. for="save-block">
						<n.exception. name="invalid_analytics_id">
							<t>Enter a valid analytics account ID.</t>
						</n.exception.>
					</n.format_error.handle_exception.>
				</then>
			</n.if.both>

			<div style="margin:1.5em 0">
				<t>Here you can use Google Analytics to measure the success of your app.</t>
				<t>Enter below your analytics account ID and you will be able to track visits, visitors and other important statistics about your web traffic.</t>
			</div>

			<n.form.>
				<t>Analytics Account ID:</t>
				<n.google_analytics_code_field.input type="text" size="20" maxlength="20"/>
				<t>(e.g., UA-12345-0)</t>
				<div style="margin-top:1.5em">
					<input type="submit" class="toolbar action-button" value="[t]Save Changes[/t]" />
					<t>or</t> <a href="[n.root_node.url/]"><t>Cancel</t></a>
				</div>
			</n.form.>
		</body>
	</n.html>
</macro>

<macro name="is_valid_analytics_id" dot_parameter="code">
	<n.either>
		<condition1.is_empty.code/>
		<condition2.regex_matches text="[n.code/]" pattern="UA-\d+-\d+"/>
	</n.either>
</macro>

<macro name="save_google_analytics_tweak">
	<n.naml_configuration.>
		<n.set>
			<name>googleAnalyticsId</name>
			<value><n.google_analytics_code_field.value/></value>
			<default></default>
			<naml>
				<![CDATA[
				<override_macro name="js_google_analytics">
					<n.overridden/>
					<script>
						ga('create', ']]><n.google_analytics_code_field.value/><![CDATA[', 'auto');
						ga('send', 'pageview');
					</script>
				</override_macro>
				]]>
			</naml>
		</n.set>
		<n.apply/>
	</n.naml_configuration.>
</macro>

<macro name="google_analytics_code_field" dot_parameter="do">
	<n.field. name="google_analytics_code"><n.do/></n.field.>
</macro>

<macro name="use_google_analytics_path">
	<n.encode_url.>
		/template/NamlServlet.jtp?macro=use_google_analytics
	</n.encode_url.>
</macro>

<macro name="use_google_analytics_link" dot_parameter="text" parameters="title, class">
	<a href="[n.use_google_analytics_path/]" class="[n.class/]" rel="nofollow" title="[n.title/]"><n.default. to="[t]Use Google Analytics[/t]"><n.text/></n.default.></a>
</macro>

<macro name="google_analytics_configurations">
	googleAnalyticsId
</macro>