• Hello,

    I’d like to collect data on users clicking on the widget button as their method of exiting the site it is hosted on. Google Analytics supports exit tracking by adding the below code to href tags, with the ‘trackOutboundLink’ function defined in my site header.

    onclick=”trackOutboundLink(‘https://www.example.com’); return false;”

    Is there a way to add this to the widget link? I tried editing into the core_actions.php script that appears to generate the href tags, but it gave a fatal error, presumably because the widget doesn’t know the function is defined elsewhere in the site.

    Halp?

    The script code (in my header) straight copied from Google is:
    <script>
    /**
    * Function that tracks a click on an outbound link in Analytics.
    * This function takes a valid URL string as an argument, and uses that URL string
    * as the event label. Setting the transport method to ‘beacon’ lets the hit be sent
    * using ‘navigator.sendBeacon’ in browser that support it.
    */
    var trackOutboundLink = function(url) {
    ga(‘send’, ‘event’, ‘outbound’, ‘click’, url, {
    ‘transport’: ‘beacon’,
    ‘hitCallback’: function(){document.location = url;}
    });
    }
    </script>

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter mtgpuzzles

    (@mtgpuzzles)

    Nevermind, I believe I figured it out using the ga(‘send”,….); function directly. I guess I’ll know when events start working.. or not. ??

    Thread Starter mtgpuzzles

    (@mtgpuzzles)

    Okay, to fully close the loop, I discovered that the issue of editing the ga function into core_actions.php is that Javascript needs the ‘ character to properly bound its variables, and php is seeing them as part of its code, so they were getting confused.

    I had to instead replace the ‘ with its ASCII code & # 0 3 9 ; (without spaces)

    The code looks totally ridiculous as a result, but (somewhat to my surprise), it worked!

    • This reply was modified 7 years, 1 month ago by mtgpuzzles.
    • This reply was modified 7 years, 1 month ago by mtgpuzzles.
    Plugin Author CodeBard

    (@codebard)

    Naturally, it will go away when updated.

    Next version of the plugin will bring a totally new plugin core, which allows every single action that happens inside the plugin to be hookable via filters and actions.

    This could allow you to modify the url by modifying it through a filter. You can place it in a plugin file of yours, so it wont go away when the original plugin (this) gets updated.

    Plugin Author CodeBard

    (@codebard)

    Hi again,

    With the new major version (2.0.0) now filtering the urls is possible. You can add any parameter by hooking to the url generating function below:

    add_filter( ‘cb_p6_filter_vars_after_make_to_patreon_url’, ‘yourfunction’);

    And your function can be like:

    function yourfunction($link)
    {

    $link= add_query_arg( ‘myparam’, ‘myvalue’, $link);

    return $link;

    }

    Thread Starter mtgpuzzles

    (@mtgpuzzles)

    Thanks. For the novice programmer, could you elaborate a little further on how exactly this would be implemented?

    Right now I’m not even using the custom function in my OP, just the built in ga function as an onclick in the href tag in core_actions.php:

    <a href … onclick=”ga(‘send’, ‘event’, ‘outbound’, ‘click’, url, {
    ‘transport’: ‘beacon’,
    ‘hitCallback’: function(){document.location = url;});” …>

    (Where in fact all my ‘ characters are currently written out as & # 0 3 9 ; )

    Would I need to actually make my own function now? From your example here, I’m a little unsure of what does what.

    Thanks!

    Plugin Author CodeBard

    (@codebard)

    Just put the filter and function in the earlier post into your theme’s functions.php, or, even better, a dud plugin just having this code.

    And in below bit:

    $link= add_query_arg( ‘myparam’, ‘myvalue’, $link);

    Change myparam to your url var, like, utm_source or utm_medium etc, whichever,

    and myvalue to the value you want.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Possible to add Google Analytics outbound traffic tracking?’ is closed to new replies.