GTM基于javascript函数触发

时间:2015-12-05 01:10:52

标签: javascript google-tag-manager

我在javascript / jquery中创建了一个应用程序......一种互动课程

我希望有一个“Lesson Finished”的触发器......

这只发生在我的javascript

中触发函数lessonFinished()时

如何在GTM中设置触发器以检测javascript中的特定功能是否被触发......或者正确的解决方法是什么?

1 个答案:

答案 0 :(得分:0)

我会使用装饰为功能添加功能。但这只有在使用全局可访问功能时才有效。您可以创建自定义HTML事件并使用类似的内容。

$(function(){
    if( typeof lessonFinished !== 'function'){
        // need to delay the execution
        console.error('lesson finished not defined');
    }
    // save the original lesson finished function
    var originalLessonFinishedFunction = lessonFinished;
    // decorate the lesson finished function with tag manager stuff
    lessonFinished = function(){

        // call the original function
        originalLessonFinishedFunction();

        // do the tag manager data layer push
        dataLayer.push({
            event: 'Lesson Finished',
            timing: Date.now(),
            extra: 'stuff'
        });
    }
});