How to hook hit counter into AMP header

Posted in: Newsmag
Post count: 9544

Hi folks
here’s a support query from me which might be rare to see 🙂

Minor item came up with the AMP roll out, where we are using the built-in hit counter (non Ajax) with Newsmag, but of course the hit function doesn’t fire off with the /amp/ CSS load redirect — basically works like a custom print CSS setup.

With one site where we still use wp-postviews, the hook was easy to add to functions.php to fire it off on each AMP visit (now getting thousands of AMP views per week on a particular site, hundreds on another!)

Seems like there would be a TD function to fire off in the same way we do the wp-postviews, via this hook
do_action( 'amp_post_template_head', 'process_postviews' );

Any pointer on the right ‘process’ to insert there for the TD hit counter, would be appreciated. I will also add that to my little AMP tutorial thread. I’ve not posted this query on that thread, as it’s more likely to be buried vs a new topic.

Thanks 🙂

(( Presumably, you could add this to a future theme update, too, but for now, need it for existing Newsmag sites where not going to update theme just for that! ))

Post count: 9544

would something like this work ?

do_action( 'amp_post_template_head', 'process_post_view_counter_key' );

or

do_action( 'amp_post_template_head', 'process_update_page_views' );

something like that ?

Post count: 9544

Or, would we want to just make a new function like AMP_counter and put in functions file, which calls all the update_post_views stuff,

then do something like

do_action( 'amp_post_template_head', 'process_AMP_counter' );

Anyway … need help when you have the time 🙂

Post count: 9544

Obviously trying to make it so we end up with

amp views = 50
desktop / mobile responsive views = 150

(*)eyeball = 200

using same count function without javascript so we don’t end up with 2 diff data fields, but ONE counter being called depending on the CSS template loaded (normal or amp).

Post count: 9544

Poke >> Radu,
when you have a free brain cell to ponder this issue ?

TIA !

Post count: 6600

Hi Chris,

Sorry for the late reply! Accustomed to your regular replies we have omitted your tread here, it happened inadvertently, we apologize for that.

I’m not sure if this is what you need but you may find theme views counter under this td_page_views class here: http://screencast.com/t/fYnyXdWjK and this class with its update_page_views method is called, which means it increments the view counters, just on the single post pages templates: http://screencast.com/t/YtOQihCe9pw

I’m not sure how the AMP version is interpreted but you may try to add a hook like this into your theme’s functions: http://screencast.com/t/wYzX3F8bhttp://pastebin.com/45icDfbD and also make sure you remove this condition: http://screencast.com/t/0EfP54gvJIKN ..do not forget about this second “{” here: http://screencast.com/t/tGvwIsek89

Give it a try and let us know how it goes!
Hope you’ll find this useful!

Thanks Chris, and again, please apologize the delay!

  • This reply was modified 10 years by Lucian.
Post count: 9544

Don’t suppose you’d be willing to switch on the amp plugin, and try the hook in functions to see if it does add it to /amp/ template via the hook provided? And see if it increments.

I’m on the front lines with this, but you’re going to get snowball of queries about this since Google will start pushing ALL its mobile traffic on compatible devices to the AMP version, and so the “counter” will not increment normally with TD themes, and some will think their audience dropped (although one can tell from the transients going way up that AMP versions being served).

I’m basically looking for working code — perhaps you’d consider adding an “tagdiv amp glue” plugin which adds the code into just the amp version via the provided amp header hook — this doesn’t need ajax since amp doesn’t use javascript.

You’re going to get a ton of queries for this — since amp is intended for news sites primarily, as are the themes — and to put it bluntly — this will actually be a superior selling point on ThemeForest to have “AMP counter support” — since almost nobody has noticed yet that their counters aren’t incrementing, but they soon will and freak out (I kind of did this the other day on the CaliforniaNewswire site when I said “hey, how come the audience dropped 70% for some of the articles …” then noticed the 9,000+ transients from the amp pages when using wp-optimize, and wen’t “oh … duh….” yeah “amp views not counted by the counter” —

Upshot – the amp view is being served as much as the mobile responsive views in the googleverse now on some of my sites, counter not incrementing. Of course, I use server side reporting (eg ‘webalizer’ etc) to know “actual” site audience views — but now the counter on all stories waaaaaaaaay down.

So – expect a bunch of newbie complaints about audience drop off once they enable AMP …. not understanding what is actually going on there.

If you can wrangle some working code on your side, for me to test – you’ll save yourself a billion support tickets for both themes as this continues to roll out !

Post count: 6600

Hi,

Thank you for this explanation Chris! I have done and tested this now and it should work. Just add this code to your theme’s functions file: http://pastebin.com/dNyJK1xchttp://screencast.com/t/YOF1P3SQh

I have checked this for the newspaper theme but it should work in the same way for the newsmag theme.

I’ll also note this so we’ll discuss this soon and consider about a plugin or a way of linking it to the theme.

Hope this helps you!

Sorry for the inconvenience!
Lucian C.

Post count: 9544

Awesome sauce!

Will give this a try and see if it also passes pesky Google AMP validation without any issues (don’t see why not).

I’ll also add link to this thread on my AMP tutorial thread.

THANKS!!

Post count: 6600

Glad that I could help you!
Let us know if you have any updates on this!

Regards!
Lucian C.

Post count: 9544

hmmmmmm…..
that didn’t seem to work, might be due to using older version of Newsmag … ( 1.7.1 ) …. doh.

ref
http://californianewswire.com/california-legislature-declares-march-2016-as-brain-injury-awareness-month/amp/

Post count: 6600

Hi Chris,

It should work with older theme versions also, however I can’t say for sure since I have only tested this on a latest version copy. I’ve also checked your site though I found that the counter is actually incrementing well now so I guess it’s working for you too.

Not sure if that was the case for you but you should also consider that using cache might affect this so you should make sure you test this without having caching on.

Let me know if you need any additional assistance on this.

Thanks

Post count: 9544

Heh. Sorry. Stupid me….
you know what I was doing to test (sure signs I need a long vacation from this stuff …. )

I was viewing the initial page, noting the view, going to amp version, then hitting back button, and going “how come it didn’t incrmenet” when obviously the back button goes back to browser cache version not new page visit.

LOL!

Using it via the actual post edit screen to see updated views, it does seem to be working.

So, guess I’m not as smart as I think I am! 🙂

Thanks again!

Chris S.

Post count: 6600

🙂 I understand! Glad you’ve found it working eventually!

Lucian C.

Viewing 14 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic.