Package: g-analytics-writer
g_analytics_writerBuild Status: ![Python package](https://github.com/jvanasco/g_analytics_writer/workflows/Python%20package/badge.svg)g_analytics_writergives lightweight support for writing optimized “Google Analytics” tracking code for those times when you need to use The Great Satan, even though you would prefer not to.Python2.7 and Python3.5+ are supportedIt offers aAnalyticsWriterobject, which provides a standard API to authoring multiple “Google Analytics” tracking formats:ga.js(historical legacy)analytics.js(current/deprecated)gtag.js(current/future)amp(accelerated mobile pages plugin)It also offers helper packages for thePyramidframework, which can automate managingAnalyticsWriterobjectsThe package is designed to work with MVC/MVT/MCT/etc systems:Python should populate all the tracking data, then render everything at once.Applications should invoke this library with ‘what I want to do’, and this library will figure out how best to do it.This is not designed to iteratively generate a tagging, but instead to generate everything at once, as optimized as possible.This package strives to create as few calls to the google servers as possible.AnalyticsWriterobjects simply contain various bits of data in an internal format, and then prints them out in the correct order via a helper functions for each format.The goal of this project is to simplify migration across versions. You tell this package what you want to track and how, it will figure out how to do that in “Google Analytics”!If you’re just using simple track pageviews, this package is likely overkillbut if you’re using any of this functionality, then its for you:custom variables for performance analyticsevent tracking for backend interaction / operationsecommerce trackingrolling up multiple domains into 1 reporting suiteThis package lets you set Google code wherever needed, and renders everything in the ‘correct’ order.Every command has extensive docstrings, which also include, credit, and link to the relevant sections of the official GoogleAnalytics API docs.Supported Concepts & CommandsCoreSetAccount and reporting into multiple domainsMultiple Domain TrackingCustom VariableseCommerceEvent TrackingSession Unification/userId (analytics.jsonly)AMP client_id integrationwhat’s the difference between all these tracking versions?There are a few big differences:custom variablesThe legacyga.jsdid not require pre-configuring the admin(online) dashboard with custom dimensions. everything was configured on the tag, from the ‘name’ to the ‘scope’.  sending data to their servers was in the form of:_gaq.push([‘_setCustomVar’,1,’pagetype’,’account’]);.Theanalytics.jsversion requires the dashboard to be pre-configured with custom dimensions. Note the form ofga(‘send’,’pageview’,{“dimension1”:”account”})does not include thepagetypelabel, only the valueaccount.Thegat.jsversion requires the dashboard to be pre-configured with custom dimensions and also requires acustom_map. Note the form ofgtag(‘set’,{“section”:”account”,”pagetype”:”home”,”is_known_user”:”1”}); gtag(‘config’,’UA-12345678987654321-12’,{“custom_map”:{“dimension1”:”section”,”dimension2”:”pagetype”,”dimension5”:”is_known_user”}});sets thepagetypelabel,however that is not transmitted to their server - it is only used locally for translation.AMP uses thegat.jsconceptsorder of execution and automatic pageviewsga.jsrequires a manual_trackPageview, so it is easy to populate the pageview with custom variables.analytics.jsrequires a manual‘send’,’pageview’, so it is easy to populate the pageview with custom variables.gtag.jsautomates‘send’,’pageview’. in order to populate the pageview with custom variables, we must either pre-populate the tracker with global variables OR disable the initial pageview and send apageview“event” to their servers.and moretransactions and events are all slightly different across versionsHistorythis package replaces the following packages,gaq_hubhttps://github.com/jvanasco/gaq_hubgaq_hubsupportedga.jstags in this repository UNDER 0.2 are the oldgaq_hubcodebasegaq_hub, replacedpyramid_gaq -https://github.com/jvanasco/pyramid_gaqpylons_gaq  -https://github.com/jvanasco/pylons_gaq| pylons support was ended in the 0.2.0 releaseAVAILABLE MODESThe available modes are:AnalyticsMode.GA_JS="legacy `ga.js`"AnalyticsMode.ANALYTICS="current/deprecated `analytics.js`"AnalyticsMode.GTAG="current/future `gtag.js`"AnalyticsMode.AMP="AMP plugin support, is a variant of `gtag.js`"The default is currentlyAnalyticsMode.ANALYTICS, which has the smallest amount of network traffic.AnalyticsMode.GTAGhas slightly larger network traffic, because thegtag.jsfile actually loads and interacts with theanalytics.jsfile.QuickStart - GeneralCreate a new AnalyticsWriter object and do stuff with it:fromg_analytics_writerimportAnalyticsWriterwriter=AnalyticsWriter('GA_ACCOUNT_ID')writer.setCustomVar(1,'TemplateVersion','A',3)printwriter.render()that’s really about itQuickStart - PyramidThePyramidhelpers simply manage aAnalyticsWriterobject in the request.gaq namespaceenvironment.ini - requiredg_analytics_writer.account_id=UA-123412341234-1234environment.ini - optionalg_analytics_writer.mode=<INTreferencesAnalyticsMode>g_analytics_writer.use_comments=<BOOLEAN>g_analytics_writer.single_push=<BOOLEANonlyforga.js>g_analytics_writer.force_ssl=<BOOLEAN>g_analytics_writer.global_custom_data=<BOOLEAN>g_analytics_writer.gtag_dimensions_strategy=<BOOLEAN>g_analytics_writer.amp_clientid_integration=<BOOLEAN>This way you can have different reporting environments.For example,dev.inimay define a secondary accountg_analytics_writer.account_id_=U-123449-2wileproduction.inidefines your primary accountg_analytics_writer.account_id_=U-123449-1You simply include the package in your__init__.pydefmain(global_config,**settings):...# custom gaqconfig.include("g_analytics_writer.pyramid_integration")When you want to set a custom variable , or anything similar…request.analytics_writer.setCustomVar(1,'TemplateVersion','A',3)Rendering Optimized VariablesForanalytics.jsthe recommended configuration option is:`global_custom_data=True`This will issue a globalsetfor all trackers before thepageviewga('create','UA-123123-1','auto');ga('set',{"dimension9":"jonathan"});ga('send','pageview');Forgtag.jsthe recommended configuration option is:`global_custom_data=True`This will issue a globalsetBEFOREissuing the config, which will automatically trigger pageviewsgtag('set',{"name":"jonathan"});gtag('config','UA-123123-1',{"custom_map":{"dimension9":"name"}});Toggling configurations can generate this:gtag('config','UA-123123-1',{"custom_map":{"dimension9":"name"},"send_page_view":false});gtag('set',{"name":"jonathan"});gtag('event','pageview');To print this out…In my mako templates, I just have this…<head>...
${request.g_analytics_writer.render()|n}
...</head>Notice that you have to escape under Mako.   For more information on mako escape options -http://www.makotemplates.org/docs/filtering.htmlLicensingThis package is made available via the MIT License –http://www.opensource.org/licenses/mit-licenseContent in the docstrings marked “Google Documentation” is copyright by Google and appears under their Creative Commons Attribution 3.0 License