How To Calculate Your Engagement Rate Using php-steem-tools

in utopian-io •  7 years ago  (edited)

You spent a lot of time networking on Steemit, commenting, upvoting and trying to create your own little community. At the end of the day, you got a few hundreds (of thousands) followers. Nice.

But how many of your readers are actually supporting you too? In other words, what's the engagement rate between the total number of your followers and the total votes you get on your posts?

In today's tutorial we're going to write a short snippet using php-steem-tools and a little magic to get exactly that. I will describe the same approach used in the "Social Insights" section of steem.supply, which generates the following chart:


Screen Shot 2018-01-03 at 9.12.47 AM.png


Prerequisites

You will need basic understanding of PHP and composer.

Step 1: Add php-steem-tools to composer.php

Install composer (this is out of the scope of this tutorial, but if you need a good start, the Composer website has a very good documentation). Open composer.php and add the following lines:

{
    "minimum-stability" : "dev",
    "require": {
        "dragos-roua/php-steem-tools": "v0.0.4-alpha"
    }
}

The current version of the package is 0.0.4-alpha, and you need the "minimum-stability" setting to be at the "dev" level.

Step 2: Require the package and instantiate the Steem API

Create a file (in the same folder as composer.php) and require the package:

require __DIR__ . '/vendor/autoload.php';

Now you'll have access to all the classes you included in the composer.php file. Let's instantiate the API. Add the following lines:

$config['webservice_url'] = 'steemd.minnowsupportproject.org';
$api = new DragosRoua\PHPSteemTools\SteemApi($config);

Now, the $api variable will contain a new API object which will query the data from the webservice_url address. If you want to query a different node, just change this address.

Step 3: Pick an author and get the total number of the followers

If you read my previous php-steem-tools tutorial, you noticed that the first two steps are pretty much identical. Here's wehre we change things a bit.

We are going to find the total number of followers of the author. This is a recursive call, hence resource intensive, so we're going to use a little folder-based cache.

Here's the code:

$follower_count  = $api->getFollowerCount($account);
$returned_follower_count = $follower_count['followers'];
$cache_folder = "./";
$file_name = $cache_folder.$author.".txt";
$cache_interval = 86400; // seconds for the cache file to live
if(file_exists($file_name)){
    
    // if the file was modified less than $cache_interval, we do nothing
    
    $current_time = time();
    if($current_time - filemtime($file_name) > $cache_interval){
        $follower_count  = $api->getFollowerCount($author);
        // write to the file again
        $handle = fopen($file_name, 'w+') or die('Cannot open file:  '.$file_name);
        fwrite($handle, "followers|".$follower_count);
        fclose($handle);
        $returned_follower_count = $follower_count;
    }
    else {
        // get the data from the cache file
        $cache_contents = file($file_name);
        $first_line = $cache_contents[0];
        $content = explode("|", $first_line);
        $returned_follower_count = $content[1];
    }
}
else {
    $follower_count  = $api->getFollowerCount($author);
    // write the data to cache file
    $handle = fopen($file_name, 'w+') or die('Cannot open file:  '.$file_name);
    fwrite($handle, "followers|".$follower_count);
    fclose($handle);
    $returned_follower_count = $follower_count;
}

It's pretty much self-explanatory, not much going on here. We make a call to the getFollowerCount() of the API and save the result in txt file. The variable $follower_count will hold the returned value from the API call.

Step 4: Get the total number of posts and the votes

The total number of posts is not really required for our goal, but it's a nice addition. Again, if you looked at my previous tutorial, you'll notice some similarities.

What we are going to do is to call the getDiscussionsByAuthorBeforeDate($params, $transport) function of the API, which will return the total number of posts during the last 8 days. If we have a result, we then cycle through the returned JSON object and the following:

  • increment the value of the variable $total_posts
  • add to the array $votes_number the voter nickname as key, and total number of his votes as the value, because we are going to use the total number of users who are actually voting
  • at the end of the loop, we just print the calculated ratio

Here's the code:

date_default_timezone_set('UTC');
$dateNow = (new \DateTime())->format('Y-m-d\TH:i:s'); 
$date_8days_ago = date('Y-m-d\TH:i:s', strtotime('-8 days', strtotime($dateNow)));
$params = [$author, '', $date_8days_ago, 100];
$remote_content = $api->getDiscussionsByAuthorBeforeDate($params, 'websocket'); 
//print_r($remote_content);
if(isset($remote_content)){
    if (array_key_exists('error', $remote_content)){
        echo "Something is not ok. You should investigate more.\n";
    }
    else 
    {
        $votes_number = array();
        $total_posts = 0;
        
        foreach($remote_content as $rkey => $rvalue){
            
            if($rvalue['pending_payout_value'] !== '0.000 SBD' && $rvalue['max_accepted_payout'] != 0){
                
                foreach($rvalue['active_votes'] as $idx => $voter){
                    $voter_nickname = $voter['voter'];
                    
                    if($voter_nickname != $author){
                        
                        if(array_key_exists($voter['voter'], $votes_number)){
                            
                            $votes_number[$voter['voter']] += 1;
                        } 
                        else {
                            
                            $votes_number[$voter['voter']] = 1;
                        }
                    }
                }
            }
            $total_posts++;
        }
        echo "\n**************\n";
        echo "Total number of posts during the last 7 days: ".$total_posts."\n";
        echo "Total number of voters: ".count($votes_number)." \n";
        echo "Total followers: ".$returned_follower_count."\n";
        
        echo "**************\n";
        echo "Conversion rate: ".number_format((count($votes_number) * 100)/$returned_follower_count, 2)."%\n**************\n";
    }
}

That's pretty much it!

Now you can invoke the script (in the same folder) by issuing this command: php conversionRate.php.

And here's how it looks for the author utopian-io:


Screen Shot 2018-01-03 at 9.00.44 AM.png


utopian-io has a 10.69% engagement rate.

You can get the full source of this tutorial from the 'examples' folder of the php-steem-tools repository on GitHub.

Note: this snippet is calculating the ratio between your followers and the number of votes you get, resulting in an overview of your popularity. It doesn't calculate how many of your followers are voting you. I leave this as a homework for you. Hint: there is a function in the api called getFollowers($account, $transport), which returns an array. You may want to intersect that array with the $votes_number array created in the script above, and get the percentage of the voting followers.


I'm a serial entrepreneur, blogger and ultrarunner. You can find me mainly on my blog at Dragos Roua where I write about productivity, business, relationships and running. Here on Steemit you may stay updated by following me @dragosroua.


Dragos Roua

If you're new to Steemit, you may find these articles relevant (that's also part of my witness activity to support new members of the platform):



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

"Social Insights" is an amazing tool of steem.supply ...Thanks @dragosroua for sharing these programming details....

very useful tool for steemit, thanks for sharing

Very useful, but I use minnowbooster sometimes for an upvote and because they use other peoples account to vote it is difficult to see whats a real vote or a paid one.

@dragosroua I love when you bring this to the table and show us this Great Stuff..........

That's pretty neat. Can come in handy, gonna check it out asap,

Thank you so much for this tool. i love the features of steem

super decent code man. a debt of gratitude is in order for including this element.Upvoted and resteemed

It's very much important to know one's daily engagement. However, it's more important to know which chunk of the audience is proactively participating in social media so that you could device your next SM strategy accordingly. Having that being said, php-steem-tools is very good tool one should have for Steemit. Also, this seems to be the first one on Steem engagement, if I'm not wrong.

Thanks @dragosroua. Definitely going to share with my audience and I hope they love it.

Steem On!

Nice! I suspect 10% is actually pretty strong given the dead followers.

Yes, anything over 10% is very good, especially if the number of followers is beyond a few thousands.

you always focus our attention on simple things that have a big impact..we always fail to notice them unless someone like you brings it for the discussion :)

This is an amzing tutorial which will definitely help me to understand #steemit in more innovative way.
Big thanks to @dragosroua who is always there to help us

Stay Blessed, Steem On!

This is interesting. Now my math and programming subjects back then now have a purpose in my life! :-P

Just kidding!

Thank you for sharing!!!

I'll definitely check out using this amazing feature at steem.supply. I wasn't aware of it before.

Thanks so much for posting more details this tool. I actually really enjoy using all the different features of steem.supply.

super nice code man. thanks for adding this feature! :)

quite awesome this is thanks for sharing time to check engagement ratio :)

I will try this after my comment on your post has went through.. Am glad steemit is growing higher since the beginning of this year

Damn, this is very useful for keeping an eye on your engagement success, thank you very much for creating this informative post!

Hello, Dragos!

It's interesting to know how to calculate the engagement rate. Thanks for the codes. Have a nice day!

its a very helpful.........../////////

great post. Code goes so far over my head I really tried to learn it but just can't rap my brain around actually understanding it. Great tutorial here!

Great work @dragosroua, going to try it as fast I can

thanks for post

Good job @dragosroua.
very helpful tips.

Helpful post..thanks for sharing it.
Upvoted and resteemed

Good good...
Pleace vote me

Very helpul tool for steem rate & calculative post!

That's a usefull tool thanks @dragosroua

Your contribution cannot be approved yet. See the Utopian Rules.

  • Contributors should not ask for Steem/Steemit related activities in their posts, such as upvotes, resteems and follows. Please edit your contribution to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Pardon me?

Please remove from your signature "You can also vote for me as witness here" to reapply for approval.

Didn't realize I have to do that, my first contribution passed with my standard signature intact. Edited, anyway.

Wow you are the first prorammer I've stubbled upon on steemit. I myself did a post on a redesign of steemit's website well pretty much a dashboard. You can check it out if you'd like. Great post btw. Do you use laravel?

  ·  7 years ago (edited)

Thanks you so much for the contribution. this tool will be really useful :)

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @dragosroua I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thanks for the info