diff --git a/gen/__init__.py b/gen/__init__.py index 8e9365c..e0ac30a 100644 --- a/gen/__init__.py +++ b/gen/__init__.py @@ -3013,4 +3013,6 @@ class Config: # When using Ogone, place an instance of appy.gen.ogone.OgoneConfig in # the field below. self.ogone = None + # When using Google analytics, specify here the Analytics ID + self.googleAnalyticsId = None # ------------------------------------------------------------------------------ diff --git a/gen/generator.py b/gen/generator.py index cdeb732..863a8c4 100644 --- a/gen/generator.py +++ b/gen/generator.py @@ -605,6 +605,7 @@ class ZopeGenerator(Generator): repls['sourceLanguage'] = self.config.sourceLanguage repls['enableSessionTimeout'] = self.config.enableSessionTimeout repls['ogone'] = repr(self.config.ogone) + repls['googleAnalyticsId'] = repr(self.config.googleAnalyticsId) repls['activateForgotPassword'] = self.config.activateForgotPassword self.copyFile('config.pyt', repls, destName='config.py') diff --git a/gen/mixins/ToolMixin.py b/gen/mixins/ToolMixin.py index 4a0fd51..a401871 100644 --- a/gen/mixins/ToolMixin.py +++ b/gen/mixins/ToolMixin.py @@ -1222,4 +1222,29 @@ class ToolMixin(BaseMixin): else: objects = method.__get__(tool)(tool) return [(o.uid, o) for o in objects] + + def getGoogleAnalyticsCode(self): + '''If the config defined a Google Analytics ID, this method returns the + Javascript code to be included in every page, allowing Google + Analytics to work.''' + # Disable Google Analytics when we are in debug mode. + if self.isDebug(): return + # Disable Google Analytics if no ID is found in the config. + gaId = self.getProductConfig().googleAnalyticsId + if not gaid: return + # Google Analytics must be enabled: return the chunk of Javascript + # code specified by Google. + code = "var _gaq = _gaq || [];\n" \ + "_gaq.push(['_setAccount', '%s']);\n" \ + "_gaq.push(['_trackPageview']);\n" \ + "(function() {\n" \ + " var ga = document.createElement('script'); " \ + "ga.type = 'text/javascript'; ga.async = true;\n" \ + " ga.src = ('https:' == document.location.protocol ? " \ + "'https://ssl' : 'http://www') + " \ + "'.google-analytics.com/ga.js';\n" \ + " var s = document.getElementsByTagName('script')[0]; " \ + "s.parentNode.insertBefore(ga, s);\n" \ + "})();\n" % gaId + return code # ------------------------------------------------------------------------------ diff --git a/gen/templates/config.pyt b/gen/templates/config.pyt index 6da84f5..e0cb77e 100644 --- a/gen/templates/config.pyt +++ b/gen/templates/config.pyt @@ -50,6 +50,7 @@ sourceLanguage = '' activateForgotPassword = enableSessionTimeout = ogone = +googleAnalyticsId = # When Zope is starting or runs in test mode, there is no request object. We # create here a fake one for storing Appy wrappers. diff --git a/gen/ui/template.pt b/gen/ui/template.pt index 2143089..0724446 100644 --- a/gen/ui/template.pt +++ b/gen/ui/template.pt @@ -34,6 +34,9 @@
+