Skip Navigation

[Resolved] Custom URL Field click tracking

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a custom post type called ads. Each Ad has several custom fields including a URL. I would like to setup tracking so that advertisers can see the number of ad clicks over time.

Solution:

It is possible with Repeatable group + some custom codes, for example:
https://toolset.com/forums/topic/custom-url-field-click-tracking/#post-1651243

Relevant Documentation:

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Robert 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1651037

Tell us what you are trying to do?

I have a custom post type called ads. Each Ad has several custom fields including a URL. I would like to setup tracking so that advertisers can see the number of ad clicks over time. I was originally thinking of adding a custom number field and have a Jquery click event to increment the field for each click but that doesn't help me with the date of the click. So I am wondering if it is a good idea to use a Repeatable group that contains a just the datetime stamp of the click instead. From there I can then count the number of datetime records on the ad and create a view for that field with a filter by date range.

Is this a good approach for something like this?

#1651043

My second question is how do I track page views on other post types I have. I am assuming it would be a simply onload event with a custom number field incremented on each load but again, it would be ideal to have a datetime stamp as well.

#1651243

Hello,

There isn't such kind of built-in feature within Toolset.

And you are right, it is possible with Repeatable group + some custom codes.

Q1) Is this a good approach for something like this?
Toolset Repeatable group is based on one-to-many relationship, each item of Repeatable group is also a post, the post type slug is same as Repeatable group slug, so you can use the post date to filter you view, see our documents:
https://toolset.com/documentation/getting-started-with-toolset/creating-and-displaying-repeatable-field-groups/
https://toolset.com/documentation/user-guides/views/filtering-views-query-by-date/

And you will need to setup custom codes:
1) insert item of Repeatable group after user click the link:
https://developer.wordpress.org/reference/functions/wp_insert_post/

2) And setup the relationship between "ads" post and new item of Repeatable group:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Q2) how do I track page views on other post types I have.

Same as above, it is possible with Repeatable group + some custom codes, you can use the post date as datetime stamp value.

#1651967

Okay I think I am going to just two single custom fields, one called "view-tracks" and the other called "click-tracks". They are both number type. The repeatable group might be overkill for what I need so we are just tracking page view and url clicks.

My issues is updating both of those fields when the post type is viewed. Since these are advertisement, they are really only even seen with a Toolset view. So the idea is that the last know value for each field is set to a var and then incremented and updated at the onload and onclick events. I know how to do this in a Toolset form but not a view. Here is my JS:

jQuery(window).bind("load", function(){
var $trackViews = jQuery('input[name=wpcf-track-views]').val();
var $trackClicks = jQuery('input[name=wpcf-track-clicks]').val();

//Increment Click Views
$trackViews++;
jQuery('input[name=wpcf-track-view]').val($trackViews);

// When Add New Button is clicked check if at max number of images
jQuery(document).on( "click", ".js-clicktrack", function(e){
$trackClicks++;
jQuery('input[name=wpcf-track-click]').val($trackClicks);
}
});
});

So there is no update code and I am not sure I am even calling the field correctly since this is not a post form. Thoughts?

#1652035

Alternatively I could add an update_post_meta call in the functions but then I don't know exactly how to call this from an onClick or onLoad event in JS. I don't think you can from there? So the perhaps adding the code to a shortcode in the view itself? I might be going in circles here:

//Increment a custom field for a given post after it is being displayed:
add_shortcode( 'track_view', 'func_track_views');
function func_track_views( $atts ) {
$atts = shortcode_atts( array(
'field' => '',
'post_id' => get_the_ID(),
), $atts );

$trackViews = get_post_meta($atts['post_id'], "wpcf-track-views", false);
$trackViews++;
update_post_meta($atts['post_id'], "wpcf-track-views", $trackViews);
}
}

#1652149

Okay this part for tracking views works:

1. Add shortcode and function to functions.php
//Increment a custom field for a given post after it is being displayed:
add_shortcode( 'track_view_click', 'func_track_views_clicks');
function func_track_views_clicks($atts) {
$atts = shortcode_atts( array(
'field' => '',
'post_id' => get_the_ID(),
), $atts );

$track = get_post_meta($atts['post_id'], "wpcf-".$atts['field'], true);
$track++;
update_post_meta($atts['post_id'], "wpcf-".$atts['field'], $track);
}

2. Add shortcode to Toolset
track_view_click

3. Add shortcode with field to increment into the loop of the view (this can be done in post as well)
[track_view_click field='track-views']

So tracking page view counts works fine.

Now, I am still having issues incrementing based on a Click event. I can call the same shortcode and use the 'track-clicks' field instead of the 'track-views' field but the question is how do I trigger the shortcode when the JS Click event occurs on the following custom field that is also part of the loop in the view?

OMG this is ridiculously easier than I thought. To track the onclick event of the url add the shortcode to an onclick event inline (onclick="[track_view_click field='track-clicks']") as follows:

4.

My issue is resolved now. Thank you!