Personal tools

Gallery2:How To Add Google Analytics to my Gallery 2

From Gallery Codex

Contents

How to Enable Google Analytics in Gallery 2

These instructions assume that you are modifying the standard Matrix theme (also works w Carbon). If you are using a different theme, the code snippets outlined below may not match yours. The most important thing to remember, is to insert the Google Analytics tracker code near the end of the html output. This is also the recommended placement by Google Analytics.

1. Copy /gallery2/themes/matrix/templates/theme.tpl into a new directory under templates/ called local. See: Editing Templates

The new copy should now reside in /gallery2/themes/matrix/templates/local/theme.tpl
This makes Gallery 2 use the copy in templates/local/ instead of the default one in templates/ and doing this ensures that any changes you have made will survive an update (either via a new Gallery 2 release or a cvs update).

2. Open /gallery2/themes/matrix/templates/local/theme.tpl and find the following:

 
    {* Put any debugging output here, if debugging is enabled *}
    {g->debug}
  </body>
</html>

3. Insert the Google Analytics javascript right above the </body> tag like this:

{* Put any debugging output here, if debugging is enabled *}
    {g->debug}
        {* Google Analytics Code *}
        <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
        </script>
        <script type="text/javascript">
        _uacct = "UA-xxxxx-x";
        urchinTracker();
        </script>
  </body>
</html>

4. Add the {literal}...{/literal} Smarty tags if you are using the new Google Analytics code (ga.js):

{* Put any debugging output here, if debugging is enabled *}
    {g->debug}
        {* Google Analytics Code *}
        {literal}
<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-xxxxxx-x");
pageTracker._trackPageview();
</script>
        {/literal}
  </body>
</html>

5. Remember to change the _uacct="UA-xxxx-x"; to your actual Analytics tracker ID.

6. Delete your template cache by clicking "run now" under Site Admin -> Maintenance -> Delete Template Cache and clicking "Clear Saved Pages" under Site Admin -> Performance.

7. Verify that the code is inserted at the right place, by browsing your Gallery 2 install and view the HTML source.

8. Watch Google Analytics give you statistics nirvana


Multisite Gallery2

If you use have multiple Gallery sites using the same theme but only wish to use Analytics for some of them, try code like this in theme.tpl (the code placement in theme.tpl would be the same as the above example):

 {capture assign="site"}{php}print basename(GALLERY_CONFIG_DIR);{/php}{/capture}
 {if $site == "gallery2" or $site == "myphotos"}
   ..code shown above for google analytics..
 {/if}

This uses the directory name for each multisite to identify whether to use analytics.


Alternate method using include

Instead of using the above methods of Analytics code inclusion on your site, and having to update the template and cache every time Google chooses to update their code, try this instead:

    {g->trailer}

    {* Put any debugging output here, if debugging is enabled *}
    {g->debug}
    {* Google Analytics code *}
    {php}
include ('/some/path/html/gallery2/ga.js');
    {/php}
  </body>
</html>

Particularly useful with multisite installs!


Add Google adsense

http://codex.gallery2.org/Gallery2:How_to_add_adsense

advertisements