Support Forum
When a new post or topic is submitted, I need a way to record this as an event in Google Analytics. I have tried two things so far:
- Utilizing the sph_post_create() action - the problem here is that no output is being sent to the page when this executes. I need to be able to create a script tag that executes the _gaq.push function to track the event.
- Using jQuery to tap into the click action on the newtopic and newpost submit buttons. Trouble here is that the page redirects before the _gaq.push function is finished executing.
Any thoughts here would be appreciated. Is there a way to queue up output in the sph_post_creation() action (short of writing something to the database, and then displaying it when the new post or topic is displayed)? Another thought was writing it to a cookie, but again, does not seem ideal.
afraid I may not be totally following what you want to do... the hook in question, sph_post_create, is fired after the post has been saved... you can do whatever you want in the function that hooks into that action, including output... however, when the hook actions complete, we will redirect to the page with the new post on it...
so not sure what you want to do to content...
are you saying the GA does not have an api that you can call from the hook?? and you must physically alter the page content? not very user friendly....
if you must, I guess I would try something like this...
iinstead of sph_post_create, hook into the filter sph_new_forum_post... the argument to the filter will be the new post object... alter the value of $p->returnURL and add a new query argument at the end - maybe &ga=xxxx
then hook into the sph_setup_forum action on the subsequent page load... this is where we are outputting page content... in that action, you can check for the query arg (ie isset($_GET['ga'])) and output what you want... or if you dont really want it in the page content, maybe try action sph_footer_end which will be in the footer of the page...
Visit Cruise Talk Central and Mr Papa's World
Thank you as always for the helpful reply. There is indeed a Google Analytics API and PHP libraries that exist for getting data into Google Analytics but they are a bear to implement (little to no documentation) and seem like overkill for what I need to do. When someone submits a new post or topic, I just need to write out the script tag that will track that action as an event in GA. Ultimately the goal is to create a report in GA that shows how many new posts and topics were submitted during a given time period.
Your second option sounds like a good possibility - creating a URL parameter that can be checked for on the page the user lands on after the post is processed. However, I went digging into the sp-post.php file a bit to see how to get to the $p->returnURL object via the sph_new_forum_post filter (line 96)... and it looks like $p->newpost is being passed into the filter, but not $p->returnURL. It does not seem like there is any way to modify $p->returnURL. The $p->newpost object does contain an empty 'url' element, but I don't think that has anything to do with returnURL.
Is there any other way to tell on the page the user is redirected to after the save that a post was just processed? From they way it looks, line 148 of sp-post.php just sends the user off to the returnURL page, and nothing else comes along with that.
No there is no way I can think of that you would know a new post was created after the redirect.
Firstly you are looking at the wrong termination point. Line 148 is for a abort condition. You need to look just above that - line 130. Now - the $newpost array 'url' element IS populated with the return url but later than you have been looking. It seems to me that what you want here is a filter (Not an action) hook that just passes the $newpost array in so that you can modify the URL. This would have to be post save of the record. So by that I really mean line 129 of the code file.
if that would be a help and a solution for you then there is no reason we can not add one. I can add it now so you know it will be there in the next update of SP.
YELLOW
SWORDFISH
|
Thanks again. Line 130 is indeed the correct spot where the returnURL is used. I'm not seeing anywhere the 'url' element in the $p->newpost array is actually used. Everything references $p->returnURL. The filter on line 96 doesn't let me get to that returnURL. So basically after line 129 (the do_action) it would be great to have something like this:
$p->returnURL = apply_filters('sph_new_forum_post_returnurl', $p->returnURL);
If you can let me know how this will be implemented so my code will still function after the next SP update, that would be perfect.
The url element IS added to the $newpost array just - as I said - a long time after where you were looking. It is added because other items record it and may need access to it.
I see no problem with what you have offered however - the name is good - and if you would like me to add that in at line 129 then I will do so and that will be there for the next update of SP.
I will put that AFTER the current Action hook.
YELLOW
SWORDFISH
|
argh, looks like I didnt dig deep enough... assume $p was the newpost array... not a class object...
but we will get it added for next release... so if you make the change yourself, should keep working once we release 5.3 down the road...
Visit Cruise Talk Central and Mr Papa's World
good news! thanks for the kind words and update!
Visit Cruise Talk Central and Mr Papa's World
1 Guest(s)