Gallery2:How To Add Google Analytics to my Gallery 2 - Gallery Codex
Personal tools

Gallery2:How To Add Google Analytics to my Gallery 2

From Gallery Codex

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. Please also note that there are two different types of Google Analytics code. The old one is called "Urchin" and the new one is referred to as "ga.js"

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 if it's the old Urchin Google Analytics code you are using:

{* 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) - or any other code that includes these signs:{}

{* 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">
try {
var pageTracker = _gat._getTracker("UA-xxxxxxxxx");
pageTracker._trackPageview();
} catch(err) {}</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, and search for "ga.js".

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):

    {g->trailer}

    {* ------ CUT HERE ----- *}

    {php}
    $includefilepath = GALLERY_CONFIG_DIR . '/ga.js';
    if (file_exists($includefilepath)) {
        include($includefilepath);
    }
    {/php}

    {* ------ CUT HERE ----- *}

  </body>
</html>

Then put a ga.js file containing the analytics javascript in the multisite install's root (next to config.php and friends). Then jump to step 6 above.

This allows every multisite user to manage their own Google Analytics javascript independently.


Add Google adsense

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