AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
2K members online now
2K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

How to add a listener to the javascript google analytics api

Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

How to add a listener to the javascript google Universal Analytics api, such that I am informed when data (pageview or event) is about to be send to Google Analytics (GA) such that I can add custom dimensions?

- Ed

1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Ed B
November 2016

Re: How to add a listener to the javascript google analytics api

Follower ✭ ✭ ☆
# 13
Follower ✭ ✭ ☆

Hi @Ed B

I understood what you want to do. Please try following code.

 

ga(function(tracker){
  var originalSendHitTask = tracker.get('sendHitTask');
  tracker.set('sendHitTask', function(model){
    if ( !model.get('dimension1') ) {
      model.set('dimension1', 'Not Set', true); // true means temporary value.
    }
    originalsendHitTask(model);
  });
});


Thanks, Ryota

View solution in original post

How to add a listener to the javascript google analytics api

Follower ✭ ✭ ✭
# 2
Follower ✭ ✭ ✭

How to add a listener to the javascript google analytics api

Follower ✭ ☆ ☆
# 3
Follower ✭ ☆ ☆

Vasim,

Thank you, but I don't understand  your answer.

Can you please explain how this is the solution to my question?

- Ed

How to add a listener to the javascript google analytics api

Explorer ✭ ✭ ☆
# 4
Explorer ✭ ✭ ☆

Hi @Ed B

 

Vasim misunderstood the question so the link is not a solution.

 

GA does not provide any other callbacks than the one received after the hit.

 

I don't quite understand why you would need it though, since custom dimensions (set or not set) are easily configured in the normal tracking code snippet.

 

Can you please give a more detailed example of what you're trying to accomplish?

How to add a listener to the javascript google analytics api

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

I don't know what is going on, but I never posted the question above. Furthermore my avatar is of my own picture, not an ape.

 

Thanks

The real Ed B

How to add a listener to the javascript google analytics api

Follower ✭ ☆ ☆
# 6
Follower ✭ ☆ ☆

Hi @Ed B,

Interesting, apparently they tagged you instead of me, no idea how that works.

However, I posted this and my name is Ed Bras, nice to meet you Smiley Wink

 

- Ed

Re: How to add a listener to the javascript google analytics api

Follower ✭ ✭ ☆
# 7
Follower ✭ ✭ ☆

Hi Ed

 

How about checking the Task Feature?

https://developers.google.com/analytics/devguides/collection/analyticsjs/tasks?hl=en

 

  function postSendHitTaskProcessing() {
    // Do anything
  }
  ga(function(tracker){
    var originalBuildHitTask = tracker.get('buildHitTask');
    tracker.set('buildHitTask', function(model){
      originalBuildHitTask(model);
      postSendHitTaskProcessing();
    });
  });

Model object has several fields, such as hitPayload, clientId, document path, hitType, ...etc.

https://developers.google.com/analytics/devguides/collection/analyticsjs/model-object-reference?hl=e...

https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en

 

Thanks.

 

 

How to add a listener to the javascript google analytics api

Follower ✭ ✭ ✭
# 8
Follower ✭ ✭ ✭

@Ed B

 

As per my understanding you are looking a solution to trigger an "Event" on Click listener to pass the hit and page view data to analytics. Is this correct ? 

 

If No, Then could you please explain your problem along with an example.

How to add a listener to the javascript google analytics api

Follower ✭ ✭ ✭
# 9
Follower ✭ ✭ ✭

@Phillip S

 

May be my understanding is not correct because of less details

How to add a listener to the javascript google analytics api

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭

As I wrote before, I never posted this question and somehow it's been linked to me. How can I remove my email address from it. I keep getting replies to a question that I never wrote. Thank you.