Awais Qarni
Awais Qarni

Reputation: 18006

Setting Custom Campaign _setCampNameKey not working in Google Analytics ga.js

Hello there I want to use some custom campaings created by my application through ga.js. I use some custome campaign and all campaign information and create a hash code of that campaign and put it in the url. My Campaigh URL looks like

http://mysite.com/?aff=eyJer5fg4IxOCIsInRleHRfbGlua19pZCI6IjgyIiwiY2FtcGFpZ25faWQiOiIxNCJ9

Now when user landed on mysite.com I check if query parameter aff exists. If yes then I decode the campaign information and try to send it as custom campaign . My tracking code looks like

 <script>
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'my profile id']);
 _gaq.push(['_trackPageview']);
 _gaq.push(['_setDomainName', 'mysite.com']);
 <?php
 if($this->campaignName != '') {
  ?>
  _gaq.push(['_setCampNameKey','<?php echo $this->campaignName?>']);//my decoded info
  _gaq.push(['_setCampMediumKey','<?php echo $this->campaignMedium?>']);//my decoded info
  <?php
  }
 ?>
 (function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
</script>

But when some user comes to mysite.com by clicking the above url, ga.js don't track the campaign. I have checked my DOM that _setCampNameKey is properly rendered. But it takes it as referral. Can any body tell me what am I doing wrong? Is it the way to track my custom campaign or some other way to do?

Upvotes: 0

Views: 757

Answers (1)

nwellnhof
nwellnhof

Reputation: 33618

Since _trackPageview triggers the tracking process, you must put the _set* commands before the _trackPageview command to be effective.

Another solution is to setup custom campaigns via URL parameters.

Edit: Also see the answer to this question. The _setCamp*Key functions don't set the value directly but the name of another URL parameter which contains the value. For example, you have to put the campaign name in a separate URL parameter like:

http://mysite.com/?camp_name=My+Campaign

Then you can tell GA that the my_camp_name contains the campaign name:

_gaq.push(['_setCampMediumKey','camp_name']);

In your case, it might work to pass a custom URL with these parameters to _trackPageview:

_gaq.push(['_setAccount', 'my profile id']);
_gaq.push(['_setDomainName', 'mysite.com']);
_gaq.push(['_setCampNameKey','camp_name']);
_gaq.push(['_setCampMediumKey','camp_medium']);
_gaq.push(['_trackPageview', 'http://mysite.com/?camp_name=<?php echo $this->campaignName?>&camp_medium=<?php echo $this->campaignMedium?>']);

Upvotes: 1

Related Questions