Mixpanel track_links不适用于动态添加的元素

时间:2013-06-11 11:27:07

标签: javascript jquery mixpanel

我在使用mixpanel.track_links时遇到动态添加的链接(页面加载后)。

对于一般示例,给定此页面:

<div id="link-div"></div>
<input type="button" id="add-link" />
<script type="text/javascript">
mixpanel.track_links(".mixpanel-event", "event name", function(ele) { return { "type": $(ele).attr("type")}});
</script>

在某些用户操作中,使用jquery将链接添加到页面。例如:

$('#add-link).click(function() {
    $('#link-div').html('<a class="mixpanel-event" type="event-type" href="#>Link to track</a>');
})

问题是单击新创建的链接时不会触发track_links。我希望有人可以分享他们使用track_link功能来动态添加链接的经验。

2 个答案:

答案 0 :(得分:7)

我很好奇所以我检查了他们的代码并继续按照他们的建议做了。我测试了它,它工作正常。这需要jQuery。

使用示例:mixpanel.delegate_links(document.body, 'a', 'clicked link');

// with jQuery and mixpanel
mixpanel.delegate_links = function (parent, selector, event_name, properties) {
    properties = properties || {};
    parent = parent || document.body;
    parent = $(parent);

    parent.on('click', selector, function (event) {
        var new_tab = event.which === 2 || event.metaKey || event.target.target === '_blank';

        properties.url = event.target.href;

        function callback() {
            if (new_tab) {
                return;
            }

            window.location = properties.url;
        }

        if (!new_tab) {
            event.preventDefault();
            setTimeout(callback, 300);
        }

        mixpanel.track(event_name, properties, callback);
    });
};

答案 1 :(得分:0)

我很难使跟踪链接按预期工作。我注意到的主要警告是,重复的事件可能会突然发送到混合面板。 我使用了稍稍的@Kyle修改版来解决我的问题。此外,这说明properties可能是mixpanel API支持的功能。

// mixpanelSetup.js
import md5 from "md5";

const setup = () => {
  mixpanel.init(TOKEN);
  // Sets ensure unique items
  mixpanel.delegated_links = new Set();
  mixpanel.delegate_links = (parent, selector, eventName, eventProperties, {ignoreUrl=false}) => {
    // Hash by whatever thing(s) the use case considers 'unique' (e.g md5(`${selector}__${eventName}`))
    const linkHash = md5(selector);
    parent = parent || document.body;
    parent = $(parent);
    // Do not add additional trackers for an already tracked event.
    if (mixpanel.delegated_links.has(linkHash)) {
      return;
    }
    mixpanel.delegated_links.add(linkHash);
    parent.on("click", selector, (event) => {
      const newTab = event.which === 2 || event.metaKey || event.target.target === "_blank";
      if (typeof eventProperties === "function") {
        eventProperties = eventProperties(event.target) || {};
      }
      eventProperties.url = event.target.href;
      // In case we don't want the url on the properties.
      if (ignoreUrl) {
        delete eventProperties.url;
      }
      const callback = () => {
        if (newTab) {
          return;
        }
        window.location = event.target.href;
      };
      if (!newTab) {
        event.preventDefault();
        setTimeout(callback, 300);
      }
      console.debug("Tracking link click!");
      mixpanel.track(eventName, eventProperties, callback);
    });
  };
}

并且可以用作:

// MyComponent.jsx
import React, { useEffect } from "react";
import { Link, useLocation } from "@reach/router";

const MyComponent = ({ moduleName, key, ...props }) => {
  const id = `#${id}__${moduleName}`;

  useEffect(() => {
    mixpanel.delegate_links(document.parent, id, event => {
      return {
          module: event.id.split("__").pop(),
          ...props.otherPropsToTrack
      };
    })
  }, [])

  return <>
    <Link {...props} to="/some/path" id={id}>My Page</Link>
  </>
}
相关问题