Fork me on GitHub

Google Analytics Modules  Bottom

  • I am starting to write a Google Analytics module. It should be fairly simple - basically store the GA code in mod vars and then use a hook to add the javascript before the tag. Basically making it independent of the theme.

    However, I am missing something about how to use hooks. I have tried to disassemble the MultiHook module (as it does something) similar but I just can't seem to get the hook to fire.

    Does anyone know where I can find some reference material on this?
  • Writing Hooks is not covered in the docs, it's actually the first item on my list for the Developer Docs side of the documentation work we're doing now. Post your code, both from pninit.php and from how it's being called, and where.

    --
    Home Page | Find on Facebook | Follow on Twitter
  • Check the /MultiHook/pnthemeapi.php, i don't know if there's a hook registered to call those functions or if the Theme Engine searchs for themeapis in the system, but that file does the job icon_wink

    --
    - Mateo T. -
    Mis principios... son mis fines
  • Hi Guys, thanks for the reply. My code is not complete. I have stuff in it to see if I can get output at all. This is what I have so far...

    The template is obvious;

    Code

    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"));


        var pageTracker = _gat._getTracker("<!--[$usercode]-->");
        pageTracker._initData();
        pageTracker._trackPageview();


    The pninit.php

    Code

    function GoogleAnalytics_Init () {
            if (!pnModRegisterHook('item',
                                   'transform',
                                   'API',
                                   'googleanalytics',
                                   'user',
                                   'transform')) {
                return LogUtil::registerError("Could not register Hook.");
                }
            return true;
            }


    then I have pnthemeapi.php

    Code

    function GoogleAnalytics_ThemeAPI_PrepareTheme () {
            PageUtil::addVar("rawtext", "Here it is rawtext....");
            PageUtil::addVar("footer", "Here it is Footer....");
            return true;
            }

        function GoogleAnalytics_ThemeAPI_Helper () {
            $out = '';
            if(SecurityUtil::checkPermission("googleanalytics::", "::", ACCESS_ADD)) {
                $output = pnRender::getInstance("googleanalytics", false);
                $output->Assign("usercode", "Code Here");
                $out = $output->Fetch("googleanalytics_footercode.html");
                }
            return $out;
            }


    Trying to copy the MultiHook theme I also have a plugin - function.googleanalyticshelper.php

    Code

    function smarty_function_googleanalyticshelper($params, &$smarty) {
            if (pnModAvailable('googleanalytics')) {
                pnModAPIFunc('googleanalytics', 'theme', 'preparetheme');
                return pnModAPIFunc('googleanalytics', 'theme', 'helper');
                }
            }


    And finally - pnuserapi.php

    Code

    function GoogleAnalytics_UserAPI_Transform ($args) {
           

                    return "la la la";

            }


    Now I just need to fit it all together.... Not sure how or why the plugin is used...
  • Well, first, i guess that to register the transform Hook is not needed here.
    That's because MultiHook transforms others modules content, but this is not the case.

    Uh! now i get how it works:
    check the file: /system/Theme/plugins/outputfilter.multihook.php
    There is invoked the APIfunc, and that outputfilter needs to be enabled in the Theme Admin Panel.

    And this part is not good: to enable the outputfilters the .ini files in the Theme should be modified to include it with some likes like:

    Code

    ...
    [filters]
    outputfilters = googleanalytics

    ...


    i found this code in the /includes/pnTheme.class.php:

    Code

    // register output filter to add MultiHook environment if requried
                    if (pnModAvailable('MultiHook')) {
                        $modinfo = pnModGetInfo(pnModGetIDFromName('MultiHook'));
                        if (version_compare($modinfo['version'], '5.0', '>=') == 1) {
                            $this->load_filter('output', 'multihook');
                            pnModAPIFunc('MultiHook', 'theme', 'preparetheme');
                        }
                    }


    So, the core is loading the MultiHook outputfilter.
    There should be a way to register an outputfilter without modify the ini files, something like a 'themeinit' Hook, so, please open a ticket and ask Frank icon_wink

    Greetings!

    --
    - Mateo T. -
    Mis principios... son mis fines
  • I agree there is a way with needing the INI file since MultiHook does not not require one.

    All the magic is in there somewhere, it is just working out how to use it!
  • Perhaps, you must take a look of "UGA" (Ultimate Google Analytics, a plugin for WordPress) for inspiration ... or adaptation icon_smile

    Personnaly, I put the GA code in my footer theme template.

    Regards.

    laurent
  • Quote

    Personnaly, I put the GA code in my footer theme template.

    I am trying to have a module where you enter your google code and it auto-magically adds the required code into the footer of what ever theme you have - no editing.

    Just need to find the time to look at this again.....!

    Anyone got some spare time they can give me?

This list is based on users active over the last 60 minutes.