Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
coding-topic
Record New Post Action in Google Analytics
Avatar
George Nagy
Member
Free Members
sp_UserOfflineSmall Offline
Feb 26, 2013 - 1:23 pm

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.  

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 26, 2013 - 10:09 pm

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...

Avatar
George Nagy
Member
Free Members
sp_UserOfflineSmall Offline
Feb 27, 2013 - 11:03 am

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.

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Feb 27, 2013 - 11:23 am

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.

andy-signature.png
YELLOW
SWORDFISH
Avatar
George Nagy
Member
Free Members
sp_UserOfflineSmall Offline
Feb 27, 2013 - 1:13 pm

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.

 

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Feb 27, 2013 - 1:28 pm

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.

andy-signature.png
YELLOW
SWORDFISH
Avatar
George Nagy
Member
Free Members
sp_UserOfflineSmall Offline
Feb 27, 2013 - 3:56 pm

Sounds good... I really appreciate you making the update!

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 27, 2013 - 6:09 pm

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...

Avatar
George Nagy
Member
Free Members
sp_UserOfflineSmall Offline
Feb 28, 2013 - 3:33 pm

I made the code update on my end and it's doing what I need.

You guys always have the answer and it's much appreciated. Thank you again! 

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Feb 28, 2013 - 3:58 pm

good news! thanks for the kind words and update!

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 619
Members: 17361
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10127
Posts: 79625