How to fix ‘get_statistics is depreciated’ problem?

I am currently working on Drupal 8.6.13. I get ‘get_statistics is depreciated’warning when working with core statistics api. I tried the following code from this question:

$  statistics = \Drupal::service('statistics.storage.node')->fetchView($  vars['node']->id()); $  totalCount  = $  statistics->getTotalCount(); $  dayCount  = $  statistics->getDayCount(); $  timestampCount  = $  statistics->getDayTimestamp(); 

But I got the followng error:

Error: Call to a member function getTotalCount() on boolean... 

Any ideas on how to fix it? Many thanks!