Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: User Analytics

  1. #1
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default User Analytics

    One of the most requested features on my site is the ability to see how many people are viewing user pages, and where they came from.

    Would it be possible to allow users to implement their own Google Analytics accounts in their accounts that tracks only their own content, so that they could simply get all this information from Analytics?

  2. #2
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default

    I was looking at the Google Analytics plug in, and also comparing it to the way the AdSense plugin works. I think it might be simple enough to create a profile field where users enter their Analytics code, use the formatting from the AdSense plugin to only use the proper Analytics code on pages the user owns, and use the Analytics plugin to actually display analytics.

    That sounds like a mouthful. Anyway, if I take a stab at building it, would anyone be willing or interested in checking it or testing it (or telling me where I went wrong)? Let me know and I'll get started as soon as I have the time.

  3. #3
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default

    hmm, couldn't I just create a new field in the user's profile, just like the documentation shows how to do with a "website", except not display it and call it "analytics" (including a description of what to do).

    Example:
    Code:
    <tr><td>Analytics Code: </td><td><input type="text" name="analytics" value="<?php echo $profile['analytics']; ?>"></td></tr>
    Of course, include the data to save this information:
    Code:
    $profile['analytics'] = $h->cage->post->('analytics');
    Then I could just add this code to the header.php file in my template:

    Code:
    public function header_include
     <?php if $h->(page belongs to page author) && (author entered their analytics code) echo {
    			
    			
    		  var _gaq = _gaq || [];
    		  _gaq.push(['_setAccount', '<?php echo $profile['analytics']; ?>']);
    		  _gaq.push(['_trackPageview']);
    
    		  (function() {
    			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    		  })();
    
    		</script>";
    	
    	}?>
    Of course, I have no idea what I'm doing so I'm probably way off.. but I just figured this would be a nice brainstorming session... if anyone is interested. This is obviously not a plugin anymore, but would accomplish the same thing I'm after. Any ideas?

  4. #4
    Former lead dev Nick's Avatar
    Join Date
    Jun 2009
    Location
    Kakamigahara, Japan
    Posts
    2,869
    Blog Entries
    88
    Thanks
    482
    Thanked 806 Times in 526 Posts

    Default

    Yeah, you're on the right lines.

  5. Thanked by:


  6. #5
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default

    Thanks Nick! Now I just need to figure out the hotaru code, determine whether or not to use header_include or "header_include_raw($h)" and elaborate a little more on the "$echo profile" so that it knows to take the author's analytics code, not the current user viewing the page. I hope someone really familiar with plugins can point me in the right track with that. 30 seconds for you geniuses is like 5 days of work and publicly destroying my site for me. A good understanding of basic php comes next. I just figured out CSS and HTML, and some Javascript (won't be visiting that again, though). At least Hotaru has simplified files. I went back to some of my old Pligg "modules" to compare code - what a nightmare.

  7. #6
    Former lead dev Nick's Avatar
    Join Date
    Jun 2009
    Location
    Kakamigahara, Japan
    Posts
    2,869
    Blog Entries
    88
    Thanks
    482
    Thanked 806 Times in 526 Posts

    Default

    Quote Originally Posted by seekyt View Post
    This is obviously not a plugin anymore
    Since it's not a plugin anymore, you wouldn't use either header_include or header_include_raw. Instead you'd just put the code directly into your header.

    You can get the current post's author id with $h->post->author, as opposed to the current user which is $h->currentUser->id.

    I'd recommend working with a test site than editing live on a production site.

  8. Thanked by:


  9. #7
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default

    Okay, so here is where I'm at. Assuming I put the Analytics form in the user's "profile"..

    Code:
    if ( !isset($h->vars['analytics'])) {
                    if ($h->post->author && !isset($h->vars['analytics']));  
                    $h->vars['analytics'] = $profile ['analytics'];
                }
    
    echo {analytics stuff goes here, with <?php echo $profile['analytics']; ?> for the "ID"}
    I'm not sure if I would need the "echo" or if I worded the "isset" part right.. Also, I want to ensure that if the "analytics" is not submitted in the user profile, no Analytics javascript will be loaded (i.e. there is no fallback analytics code. Analytics only works if the post author entered their ID). Finally, when I use
    Code:
    <?php echo $profile['analytics']; ?>
    within the actual analytics script, it would be surrounded by single quotes (i.e. 'the php echo') instead of double (i.e. "the php echo"), right?

    Sorry guys, I know you're not teachers and have better things to do. Thanks for all your help.

  10. #8
    Former lead dev Nick's Avatar
    Join Date
    Jun 2009
    Location
    Kakamigahara, Japan
    Posts
    2,869
    Blog Entries
    88
    Thanks
    482
    Thanked 806 Times in 526 Posts

    Default

    You don't need the second !isset($h->vars['analytics'] because you've already established that it isn't set in the opening if condition.

    To ensure you echo single quotes instead of double quotes, you could try something like this:

    PHP Code:
    $profile['analytics'] = str_replace("\"""'"$profile['analytics']); 
    That should convert any double quotes to single quotes.

    Aside: I enjoy these kind of threads and don't mind helping. They are much more productive than "I want to build a space rocket with Hotaru. How can I do it?"

    Aside 2: Actually, I am a teacher... I teach English to Japanese kids.

  11. #9
    Donor
    Join Date
    Sep 2010
    Posts
    214
    Thanks
    29
    Thanked 54 Times in 39 Posts

    Default

    That's awesome, Nick. I can tell, not only from this thread but from all over the Hotaru forums, that you must be a great teacher.

    Okay, so from everything I have so far, this is the code I'm looking at adding to the header:

    Code:
    if (!isset($h->vars['analytics'])) {
                    if ($h->post->author);  
                    $h->vars['analytics'] = $profile ['analytics'];
                    $profile['analytics'] = str_replace("\"", "'", $profile['analytics']);  
                }
    echo {	
    		  var _gaq = _gaq || [];
    		  _gaq.push(['_setAccount', '<?php echo $profile['analytics']; ?>']);
    		  _gaq.push(['_trackPageview']);
    
    		  (function() {
    			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    		  })();
    
    		</script>";
    	
    	}?>
    And the code I will be adding to edit_profile.php (in the right spots):
    Code:
    <tr><td>Analytics Code: </td><td><input type="text" name="analytics" value="<?php echo $profile['analytics']; ?>"></td></tr>
    
    $profile['analytics'] = $h->cage->post->('analytics');
    I understand that this is not the most elegant method, but here is what all of this should achieve so far:


    • If the author of the current page has entered their analytics ID in their profile, the Analytics tracker will be activated.

    • If the author of the current page has NOT entered their analytics ID, nothing will happen

    • The analytics ID will be displayed as UA-12345-1 (example), surrounded by single quotes only.


    Now, before I figure out how to set up a test environment (i'm thinking of just copying everything over to a subfolder (i.e. "demo") and loading the site from there (i.e. site.com/demo).. is that how you set up test sites?), does all this look like it will, at least, logically work? I'm very cautious about simple syntax errors at this point because it probably won't be apparent to me if something is off or not. Thanks again for your help Nick (and anyone else who decides to comment )

    Also, now that I'm looking at it, should I place <?php before that entire block of code, since it ends with ?> ?

  12. #10
    Former lead dev Nick's Avatar
    Join Date
    Jun 2009
    Location
    Kakamigahara, Japan
    Posts
    2,869
    Blog Entries
    88
    Thanks
    482
    Thanked 806 Times in 526 Posts

    Default

    I have a test site set up on my computer, as opposed to on the net. You can do that with WampServer. Otherwise, create a new Hotaru site in a sub-domain, e.g. dev.example.com.

    PHP should start with <? and finish with ?>

    This won't work because you're missing a tester after "post->":

    PHP Code:
    $profile['analytics'] = $h->cage->post->('analytics'); 
    Read up on using Inspekt here:
    http://docs.hotarucms.org/index.php/...pekt_Functions

    It's difficult to say whether the code will work or not until you start testing it.

  13. Thanked by:


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Google Analytics 0.5
    By carlo75 in forum Plugin Downloads
    Replies: 25
    Last Post: 03-29-2011, 11:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •