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?
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- michiel responded to »password problem« 10:01 AM
- mazdev responded to »Hide "Register new account" and change template to 3 col« 07:50 AM
- mesteele101 created topic »Zikula 1.3.3 - Site Search 1.5.2 - Unable to turn off plug-ins« 07:48 AM
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 25. May
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 25. May
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 25. May
- mdee created topic »How to implement returnpage ?« 25. May
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
Google Analytics Modules
-
- Rank: Registered User
- Registered: Apr 02, 2002
- Last visit: Dec 13, 2009
- Posts: 32
-
- Rank: Developer
- Registered: Dec 31, 1969
- Last visit: Jun 01, 2010
- Posts: 6859
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
-
- Rank: Team Member
- Registered: Sep 06, 2006
- Last visit: May 09, 2010
- Posts: 2446
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
--
- Mateo T. -
Mis principios... son mis fines -
- Rank: Registered User
- Registered: Apr 02, 2002
- Last visit: Dec 13, 2009
- Posts: 32
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... -
- Rank: Team Member
- Registered: Sep 06, 2006
- Last visit: May 09, 2010
- Posts: 2446
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
Greetings!
--
- Mateo T. -
Mis principios... son mis fines -
- Rank: Registered User
- Registered: Feb 11, 2009
- Last visit: Jul 30, 2009
- Posts: 10
Perhaps, you must take a look of "UGA" (Ultimate Google Analytics, a plugin for WordPress) for inspiration ... or adaptation
Personnaly, I put the GA code in my footer theme template.
Regards.
laurent -
- Rank: Registered User
- Registered: Apr 02, 2002
- Last visit: Dec 13, 2009
- Posts: 32
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?
- Moderated by:
- Support
