103

I'm implementing some code to do my own referral tracking on downloads from the Android market.

See Android referral tracking does not work for an idea of what my app is doing.

How can I test if this code is working before deploying to the public?

Community
  • 1
  • 1
emmby
  • 99,783
  • 65
  • 191
  • 249

8 Answers8

267

The easiest way is using adb. You don't have to write any code.

Just run in a terminal:

adb shell 
am broadcast -a com.android.vending.INSTALL_REFERRER -n <your.package>/.<path.up.until.your.BroadcastReceiver> --es "referrer" "utm_source=test_source\&utm_medium=test_medium\&utm_term=test_term\&utm_content=test_content\&utm_campaign=test_name"

Here's my exact line:

am broadcast -a com.android.vending.INSTALL_REFERRER -n net.lp.collectionista/.util.broadcast_receivers.FacadeBroadcastReceiver --es "referrer" "utm_source=test_source\&utm_medium=test_medium\&utm_term=test_term\&utm_content=test_content\&utm_campaign=test_name"

But your BroadcastReceiver may need to be the AnalyticsReceiver, i.e.

For Google Analytics v2:

com.your.package/com.google.analytics.tracking.android.CampaignTrackingReceiver

For Google Analytics v3:

com.your.package/com.google.android.apps.analytics.AnalyticsReceiver

For Google Analytics v4:

com.your.package/com.google.android.gms.analytics.CampaignTrackingReceiver

As Luigi said, you can also leave out the "-n" componentname part, but then every app on your device will receive the referral. This can be a good extra test to see if your BroadcastReceiver can be found properly.

The output I see (especially the last line is important):

05-13 17:28:08.335: D/Collectionista FacadeBroadcastReceiver(8525): Receiver called
05-13 17:28:08.335: V/Collectionista FacadeBroadcastReceiver(8525): Receiver called with action: com.android.vending.INSTALL_REFERRER
05-13 17:28:08.365: D/GoogleAnalyticsTracker(8525): Stored referrer:utmcsr=test_source|utmccn=test_name|utmcmd=test_medium|utmctr=test_term|utmcct=test_content
Mykola
  • 3,343
  • 6
  • 23
  • 39
pjv
  • 10,658
  • 6
  • 43
  • 60
  • what sample output u get can u add here – Khan May 04 '12 at 09:26
  • @pjv i have test app get output but if i donwload app from play store than output is null can u exmplain little bit more – Khan May 04 '12 at 10:45
  • @Khan, yeah, mine also has issues in real life, even problems in general with Google Analytics. Especially when you have a complex app and maybe more than one BroadcastReceiver. – pjv May 05 '12 at 00:56
  • 2
    If you use a static inner class as a BroadCastReceiver you have to escape the dollar sign. In my case I had to leave out the dot before the path up to the BroadCastReceiver. "am broadcast -a com.android.vending.INSTALL_REFERRER -n app.test/app.util.AndroidUtil\$TestBroadCastReceiver" – Geert Weening Aug 29 '12 at 10:20
  • 6
    After lots of trial and error I found that if your Receiver is in a library, you need to omit the . after / -- so the example would look like this: adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n / --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name" – bkurzius Apr 04 '13 at 18:57
  • 4
    +1 for `adb shell am broadcast..`. I type `adb shell am broadcast...` by mistake and it doesn't output anything. – anticafe Aug 27 '14 at 14:45
  • When I tried using my own custom receiver, it's working when running the test above, **but not in the real implementation**. After having my app published on Play Store, I failed to get the referrer parameters. I wonder if Analytics Receiver has to be used instead. But then, how to collect the referral data? – Harry Jan 28 '15 at 07:43
  • @pjv When i used it in live app, (not set) is shown in analytics.So is there something wrong in my development side? – KP_ Oct 09 '15 at 05:11
  • How did you get 05-13 17:28:08.365: D/GoogleAnalyticsTracker(8525): Stored referrer:utmcsr=test_source|utmccn=test_name|utmcmd=test_medium|utmctr=test_term|utmcct=test_content – Atul O Holic Dec 23 '15 at 07:51
  • 1
    An important note is that all the ampersands(&) inside the referrer string should be prefixed with backspace (\) or else the string will be terminated at the first ampersand and won't reach the broadcast receiver. (resulting in only getting the first pair in this case "utm_source=utm_test") – Nati Dykstein Jan 10 '16 at 12:32
  • I battled with this too. The issue for me was is we were changing the package name to add the suffix .dev in the gradle for the build. I was trying to use that modified package name since that was essentially the new name. I was also running it as a debug build. It would successfully show in adb, but never trigger in the app. Once I tried it with the non modified package name in the production version, it triggers every time. – a54studio May 03 '16 at 20:06
  • 2
    "But your BroadcastReceiver may need to be the AnalyticsReceiver" what do you mean by that ? What If I wantto have my own custom reciever – Utsav Gupta Jun 29 '16 at 08:32
  • In my case adb shell and am command in one line is working with enter its not working. – Khizar Hayat Nov 15 '17 at 08:35
  • how to send this campaign data to google analytics console using my tracking id? – Rahul Bansal Jul 20 '18 at 12:56
  • 1
    I had to use full package string to get it to work. i.e. `net.lp.collectionista/net.lp.collectionista.util.broadcast_receivers.FacadeBroadcastReceiver`. – Sachin Chandil Feb 04 '19 at 14:39
19

No! you have a few way to test it Send a broadcast manually with an intent of this form

    Intent i = new Intent("com.android.vending.INSTALL_REFERRER");
    //Set Package name
    i.setPackage("com.package.yourapp");
    //referrer is a composition of the parameter of the campaing
    i.putExtra("referrer", referrer);
    sendBroadcast(i);
Luigi Agosti
  • 925
  • 10
  • 19
10

None of the above commands works for me. After trying a lot of commands combinations here is the one that works for me:

./adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n <your package>/<your package>.<your BroadcastReceiver> --es "referrer" "utm_medium%3Dpartner%26utm_campaign%3Dpartner_name"

Note: the referrer should be url encoded.

Pablo
  • 1,368
  • 12
  • 18
  • Im getting "not set" as medium , campaign value in analytics dashboard.Only source name is displayed .Can you figure out what may be the problem? – KP_ Oct 14 '15 at 11:39
6

pjv's answer works in the case where the package name in AndroidManifest.xml matches the applicationId in build.gradle. If they don't match, do the following:
Given:
applicationId is com.my.app.debug
package is com.package.app
receiver is path.to.MyReceiver
then broadcast to com.my.app.debug/com.package.app.path.to.MyReceiver
Details here: https://groups.google.com/forum/#!topic/adt-dev/PjTHX79Iomw

AtomicBoolean
  • 1,070
  • 13
  • 19
4

Found a nice open-source tool that lets you scan the referral qrcode that you generate here, and it sends a broadcast intent with all the correct information.

https://github.com/giago/referraltester

Gyuri
  • 101
  • 2
  • 5
0

https://github.com/rogerbinns/referraltester here is a great app for initiate "com.android.vending.INSTALL_REFERRER" broadcast. you can input parameters as well.

sammiwei
  • 3,140
  • 9
  • 41
  • 53
0

For me extras added to the intent only when adding with "-e" instead of "--es". Might be my ad version issue. So my command is ./adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n <your package>/<your package>.<your BroadcastReceiver> -e "referrer" "utm_medium%3Dpartner%26utm_campaign%3Dpartner_name"

Sojan P R
  • 536
  • 6
  • 9
0

Goople Play receives an APP link and asks the user to click to download, and Google Play sends a broadcast when the download is complete. APP link will usually take some parameters (such as the following example) to let Google Play and APP know where the source comes from.

Https://play.google.com/store/apps/details?id=com.example.application
& Referrer = utm_source% 3Dgoogle
% 26utm_medium% 3Dcpc
% 26utm_term% 3Drunning% 252Bshoes
% 26utm_content% 3Dlogolink
% 26utm_campaign% 3Dspring_sale

So we can use adb to simulates the broadcast from Google Play.

echo 'am broadcast \
    -a com.android.vending.INSTALL_REFERRER \
    -n "com.google.samples.quickstart.analytics/com.google.android.gms.analytics.CampaignTrackingReceiver" \
    --es "referrer" \
      "utm_source=test_sourceCampaignTrackingReceiver&utm_medium=referral&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; \
    exit' | adb shell
WeiYuan
  • 5,922
  • 2
  • 16
  • 22