0

I have found an interesting article about tracking variables with JS based statical system (for example Google Analytics).

The article writes examples like:

_gaq.push('setCustomVar', 3, 'pubDate','YYYYMM',3);
_gaq.push('setCustomVar', 4, 'contCat','[CATEGORY NAME]',3);
_gaq.push('setCustomVar', 5, 'contSubCat','[SUB CATEGORY NAME]',3);

But how can I get the actual value of the pubdate from the PHP environment of Wordpress and write it in the HTML generated code so that it will be really made available to analytics? I hope the question is clear enough.

1 Answer 1

1

If you're printing your script inline (i.e. not in an external file), you can use PHP to dynamically output the JavaScript argument:

<script>
    _gaq.push( 'setCustomVar', 3, 'pubDate', '<?php the_time( 'Ym' ) ?>', 3 );
</script>

See the PHP manual for date arguments, which will explain why I've used Ym.

2
  • the_time is the WP function for getting when it was published? very nice.. If you don't mind to modify your answer I will edit the question to ask also for the category so that it will become even more useful
    – Revious
    Apr 24, 2015 at 17:02
  • It's possible to have multiple categories per post, so you'd need to decide how to handle that, e.g. only display one. get_the_category() gives you all the details – id, name, slug, parent etc. – in an array, so you can extract the information you need. Apr 24, 2015 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.