Alfredo Gallegos
Alfredo Gallegos

Reputation: 643

Send extra strings when tracking outbound links with Google Analytics

I'm developing an integration for Google Analytics on my website.

The idea is that a User can see the top 3 most clicked links in a leaderboard kind of widget that I'm going to show on the homepage.

I am successfully tracking outbound link clicks with Google Analytics using this little snippet:

var trackJobPostingLinkClick = function(url) {
       ga('send', 'event', 'outbound', 'click', url, {
         'transport': 'beacon',
         'hitCallback': function(){document.location = url;}
       });
}

And by triggering this function whenever a user clicks one of the links in my Job Posting Links:

<a href="https://link1" onclick="trackJobPostingLinkClick('https://link1'); return false;\">JobTitle</a>

However, I'm really interested into sending the JobTitle string (which is unique to each link) whenever I track the click because to make it a bit nicer on the eyes when I display it on my leaderboard. Is there any way that I can send the JobTitle together with the click?

So far the data I'm getting back from the API looks a bit like this:

Request:

https://www.googleapis.com/analytics/v3/data/ga?ids=[GA ID]&start-date=30daysAgo&end-date=2017-08-10&metrics=ga%3AtotalEvents&dimensions=ga%3AeventCategory%2Cga%3AeventAction%2Cga%3AeventLabel&sort=-ga%3AtotalEvents&max-results=3&access_token=[TOKEN]

Response:

{
    ... lots of metadata...
    "rows": [
        [
            "outbound",
            "click",
            "https://link1",
            "10"
        ],
        [
            "outbound",
            "click",
            "https://link2",
            "8"
        ],
        [
            "outbound",
            "click",
            "https://link3",
            "8"
        ]
    ]
}

Ideally I'd get something back from this api like this

{
        ... lots of metadata...
        "rows": [
            [
                "outbound",
                "click",
                "https://link1",
                "Link 1 Title",
                "10"
            ],
            [
                "outbound",
                "click",
                "https://link2",
                "Link 2 Title",
                "8"
            ],
            [
                "outbound",
                "click",
                "https://link3",
                "Link 3 Title",
                "8"
            ]
        ]
    }

Which I could easily display on the leaderboard. Can anybody point me in the right direction? Is this possible?

Upvotes: 0

Views: 134

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32780

Google Analytics standard offers 20 custom dimensions (Google 360 has 200) - these are data fields that you can name yourself and use them to enrich your data.

Custom Dimensions come in various "scopes" - hit, session, user and product; your use case would be a hit scoped dimension, where the data field is attributed to each event you send (session scope would only hold the last value in a visit, user scope only the last value for a user).

You have to create custom dimension in the GA admin panel in the property settings (look under "custom definitions"). You can assign a name that is used in the reporting interface. However to address the custom dimension in the tracking code you use the numeric index (basically the order in which your custom dimensions where created).

var trackJobPostingLinkClick = function(url) {
       ga('send', 'event', 'outbound', 'click', url, {
         'dimension1: <your value>
         'transport': 'beacon',
         'hitCallback': function(){document.location = url;}
       });
}

Custom Dimensions do not show up in the standard reports by default, but you can use them as secondary dimensions, in custom reports, via the API or in data studio (and you can also use them for segmentation or in view filters).

Upvotes: 2

Related Questions