Forums

Google Analytics for WordPress
Filter to use for custom links (2 posts)

  1. magicroundabout
    Member
    Posted 3 months ago #

    Hi there,

    I'm having trouble with some downloads being tracked. I think it's to do with the fact that my download links are generated from post meta data. So I do something like:

    $url = get_post_meta( get_the_ID(), '_some_meta_var', true );
    printf('<a href="%s>%s</a>, $url, "My download");

    I'd assumed that GA4WP would use JavaScript to scan the page for links and inject the relevant "onClick" code, or just use a function that triggers on any anchor tag but it seems that it doesn't work this way.

    From looking at the GA4WP code it seems that you filter links in the_content (and various other places) and inject the tracking code there, for each link. This won't, obviously, apply to my custom download links.

    Is there, therefore, a recommended way of tracking my links. Perhaps by applying one of the GA4WP filters to my links before printing them. Something like:

    $url = get_post_meta( get_the_ID(), '_some_meta_var', true );
    $link = printf('<a href="%s>%s</a>, $url, "My download");
    if (class_exists('GA_Filter')) {
      $link = GA_Filter::the_content($link);
    }

    Is this the kind of thing I should be doing? Or is there a better way?

    Thanks

    http://wordpress.org/extend/plugins/google-analytics-for-wordpress/

  2. magicroundabout
    Member
    Posted 3 months ago #

    Minor correction in second code snippet - should be sprintf:

    $url = get_post_meta( get_the_ID(), '_some_meta_var', true );
    $link = sprintf('<a href="%s>%s</a>, $url, "My download");
    if (class_exists('GA_Filter')) {
      $link = GA_Filter::the_content($link);
    }

Reply

You must log in to post.

About this Plugin

About this Topic