I'm trying to track visits to specific topics on my website. Each article can be tagged with several topics so it seems the only way to track visits per topic is by firing an event for each topic the article is in.
I am using Google Analytics (UA) and Google Tag Manager.
So far I have created the following as a Custom HTML Tag in GTM:
<script>
var wt_topics = $("meta[name='WT.cg_n']").attr("content");
var topic_array = wt_topics.split(';');
var uniqueTopics = [];
$.each(topic_array, function(i, el){
if($.inArray(el, uniqueTopics) === -1) uniqueTopics.push(el);
});
var recordTitle = $("meta[name='DCSext.recordtitle']").attr("content");
$.each(uniqueTopics, function( index, value ) {
ga('send', 'event', 'Topics', value, recordTitle);
});
</script>
This converts a string available on the webpage into a JavaScript array of topics, then it removes any duplicates, and finally fires an event for each item in the array. Or at least, it is supposed to do that last bit.
If I replace the ga('send'... code with an alert() tag it works, so I suspect it doesn't know what the term 'ga' is supposed to.
What am I doing wrong? and is there a better way to do this, using GTMs macros, tags, etc?
Thanks