Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixpanel track_links does not work with dynamically added elements

I'm having trouble using mixpanel.track_links with links added dynamically (after page load).

For a general example, given this page:

<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>

At some user action, links are added to the page using jquery. For example:

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

The problem is that track_links isn't triggered on click of the newly created link. I'm hoping someone can share their experience in enabling the track_link function to work for dynamically added links.

like image 325
ray.dino Avatar asked Jun 11 '13 11:06

ray.dino


People also ask

What is the difference between Mixpanel track_forms and track_links?

mixpanel.track_forms - similar to track_links, but tracks form submissions. By default, Mixpanel cookies send over HTTPS requests as part of the headers. However, Mixpanel’s JavaScript library provides a configuration to completely prevent the browser from transmitting your cookies with non-HTTPS requests.

How do I send a song event to Mixpanel //?

// Send a "Played song" event to Mixpanel // with a property "genre" mixpanel.track ( "Played song", {"genre": "hip-hop"} ); All events sent from the JavaScript library will send over HTTPS. When tracking link clicks with mixpanel.track, the page can change before the event is successfully sent, leading to inaccurate results.

What is Mixpanel group analytics?

Mixpanel Group Analytics allows behavioral data analysis by selected groups, as opposed to individual users. Grouping by identifiers other than the distinct_id will allow analysis at a company or group level when using Mixpanel analytics. Read this article to learn more about Group Analytics.

How does Mixpanel handle user opt-out?

Mixpanel’s tracking libraries will send user data by default. Explicitly initializing a default opt-out state of true will opt-out all users by default, preventing data from sending unless a user’s opt-out state is set to false. Mixpanel’s Javascript library also respects browser Do Not Track settings.


1 Answers

I was curious so I checked out their code and went ahead and did as they suggested. I tested it, and it worked fine. This requires jQuery though.

Example usage: 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);
    });
};
like image 165
Kyle Avatar answered Sep 26 '22 00:09

Kyle